Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadHandoverDraft.test.ts

65 lines2.5 KB

1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 clearComposerDraftContentState,
6 mergeComposerDraftContentState,
7} from "./use-composer-drafts";
8import { threadHandoverDraftImportId } from "./threadHandoverDraft";
9​
10describe("threadHandoverDraftImportId", () => {
11 it("keeps retries idempotent across restart and allows another import after clear", () => {
12 const environmentId = EnvironmentId.make("environment-1");
13 const threadId = ThreadId.make("thread-1");
14 const destinationDraftKey = "new-task:environment-1:project-1";
15 const firstImportId = threadHandoverDraftImportId(environmentId, threadId);
16 const written = mergeComposerDraftContentState({}, destinationDraftKey, {
17 text: "Generated handover",
18 attachments: [],
19 sourceShareId: firstImportId,
20 });
21 const edited = {
22 [destinationDraftKey]: {
23 ...written[destinationDraftKey]!,
24 text: "Edited generated handover",
25 workspaceSelection: {
26 mode: "worktree" as const,
27 branch: "user-edited",
28 worktreePath: "/worktrees/edited",
29 },
30 },
31 };
32​
33 // A restarted process reconstructs the same receipt from the source
34 // thread. Regeneration cannot append over the user's edited draft.
35 const restartedImportId = threadHandoverDraftImportId(environmentId, threadId);
36 expect(restartedImportId).toBe(firstImportId);
37 expect(
38 mergeComposerDraftContentState(edited, destinationDraftKey, {
39 text: "Regenerated handover",
40 attachments: [],
41 sourceShareId: restartedImportId,
42 }),
43 ).toBe(edited);
44 expect(edited[destinationDraftKey]?.workspaceSelection?.branch).toBe("user-edited");
45​
46 // Clear/send removes import receipts, making the same source eligible for
47 // a deliberate new handover draft.
48 const cleared = clearComposerDraftContentState(edited, destinationDraftKey);
49 const retried = mergeComposerDraftContentState(cleared, destinationDraftKey, {
50 text: "Regenerated handover",
51 attachments: [],
52 sourceShareId: restartedImportId,
53 });
54 expect(retried[destinationDraftKey]).toMatchObject({
55 text: "Regenerated handover",
56 importedShareIds: [restartedImportId],
57 });
58 });
59​
60 it("keeps opaque environment and thread IDs unambiguous", () => {
61 expect(threadHandoverDraftImportId(EnvironmentId.make("a:b"), ThreadId.make("c"))).not.toBe(
62 threadHandoverDraftImportId(EnvironmentId.make("a"), ThreadId.make("b:c")),
63 );
64 });
65});