|
| 1 | +import { |
| 2 | + EnvironmentId, |
| 3 | + MessageId, |
| 4 | + ProviderInstanceId, |
| 5 | + ThreadId, |
| 6 | + TurnId, |
| 7 | + type OrchestrationMessage, |
| 8 | +} from "@t3tools/contracts"; |
| 9 | +import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 10 | + |
| 11 | +const drafts = vi.hoisted(() => ({ |
| 12 | + setModelSelection: vi.fn(), |
| 13 | + setRuntimeMode: vi.fn(), |
| 14 | + setInteractionMode: vi.fn(), |
| 15 | + setPrompt: vi.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("./composerDraftStore", () => ({ |
| 19 | + useComposerDraftStore: { getState: () => drafts }, |
| 20 | +})); |
| 21 | +vi.mock("./state/entities", () => ({ readThreadDetail: vi.fn() })); |
| 22 | +vi.mock("./state/threads", () => ({ |
| 23 | + environmentThreadDetails: { detailAtom: vi.fn(() => Symbol("detail")) }, |
| 24 | +})); |
| 25 | +vi.mock("./rpc/atomRegistry", () => ({ |
| 26 | + appAtomRegistry: { subscribe: vi.fn() }, |
| 27 | +})); |
| 28 | + |
| 29 | +import type { DraftId } from "./composerDraftStore"; |
| 30 | +import { appAtomRegistry } from "./rpc/atomRegistry"; |
| 31 | +import { readThreadDetail } from "./state/entities"; |
| 32 | +import { continueThreadInNewDraft } from "./threadContinuation"; |
| 33 | + |
| 34 | +const threadRef = { |
| 35 | + environmentId: EnvironmentId.make("environment-1"), |
| 36 | + threadId: ThreadId.make("thread-1"), |
| 37 | +}; |
| 38 | +const draftId = "draft-1" as DraftId; |
| 39 | + |
| 40 | +function message(role: OrchestrationMessage["role"], text: string): OrchestrationMessage { |
| 41 | + return { |
| 42 | + id: MessageId.make(`message-${role}`), |
| 43 | + role, |
| 44 | + text, |
| 45 | + turnId: TurnId.make("turn-1"), |
| 46 | + streaming: false, |
| 47 | + createdAt: "2026-08-28T00:00:00.000Z", |
| 48 | + updatedAt: "2026-08-28T00:00:00.000Z", |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +function sourceThread(messages: ReadonlyArray<OrchestrationMessage>) { |
| 53 | + return { |
| 54 | + title: "Source thread", |
| 55 | + modelSelection: { |
| 56 | + instanceId: ProviderInstanceId.make("claude"), |
| 57 | + model: "claude-opus-5", |
| 58 | + options: [{ id: "effort", value: "high" }], |
| 59 | + }, |
| 60 | + runtimeMode: "full-access" as const, |
| 61 | + interactionMode: "plan" as const, |
| 62 | + messages, |
| 63 | + } as unknown as NonNullable<ReturnType<typeof readThreadDetail>>; |
| 64 | +} |
| 65 | + |
| 66 | +describe("continueThreadInNewDraft", () => { |
| 67 | + beforeEach(() => { |
| 68 | + vi.clearAllMocks(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("creates a draft with source context and source provider state", async () => { |
| 72 | + vi.mocked(readThreadDetail).mockReturnValue( |
| 73 | + sourceThread([message("user", "Diagnose it"), message("assistant", "Found the cause")]), |
| 74 | + ); |
| 75 | + const createDraft = vi.fn(async () => ({ draftId })); |
| 76 | + |
| 77 | + const result = await continueThreadInNewDraft({ threadRef, createDraft }); |
| 78 | + |
| 79 | + expect(result).toEqual({ ok: true }); |
| 80 | + expect(createDraft).toHaveBeenCalledOnce(); |
| 81 | + expect(drafts.setModelSelection).toHaveBeenCalledWith( |
| 82 | + draftId, |
| 83 | + expect.objectContaining({ instanceId: "claude", model: "claude-opus-5" }), |
| 84 | + { replaceOptions: true }, |
| 85 | + ); |
| 86 | + expect(drafts.setRuntimeMode).toHaveBeenCalledWith(draftId, "full-access"); |
| 87 | + expect(drafts.setInteractionMode).toHaveBeenCalledWith(draftId, "plan"); |
| 88 | + expect(drafts.setPrompt).toHaveBeenCalledWith( |
| 89 | + draftId, |
| 90 | + expect.stringContaining("### Assistant\n\nFound the cause"), |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it("loads uncached thread detail through the subscribed atom", async () => { |
| 95 | + const source = sourceThread([message("user", "Load this context")]); |
| 96 | + vi.mocked(readThreadDetail).mockReturnValue(null); |
| 97 | + const dispose = vi.fn(); |
| 98 | + vi.mocked(appAtomRegistry.subscribe).mockImplementation((_atom, listener) => { |
| 99 | + listener(source); |
| 100 | + return dispose; |
| 101 | + }); |
| 102 | + |
| 103 | + const result = await continueThreadInNewDraft({ |
| 104 | + threadRef, |
| 105 | + createDraft: async () => ({ draftId }), |
| 106 | + }); |
| 107 | + |
| 108 | + expect(result).toEqual({ ok: true }); |
| 109 | + expect(dispose).toHaveBeenCalledOnce(); |
| 110 | + expect(drafts.setPrompt).toHaveBeenCalledWith( |
| 111 | + draftId, |
| 112 | + expect.stringContaining("Load this context"), |
| 113 | + ); |
| 114 | + }); |
| 115 | + |
| 116 | + it("does not create a draft when no completed portable context exists", async () => { |
| 117 | + vi.mocked(readThreadDetail).mockReturnValue(sourceThread([message("system", "hidden")])); |
| 118 | + const createDraft = vi.fn(async () => ({ draftId })); |
| 119 | + |
| 120 | + const result = await continueThreadInNewDraft({ threadRef, createDraft }); |
| 121 | + |
| 122 | + expect(result).toMatchObject({ ok: false }); |
| 123 | + expect(createDraft).not.toHaveBeenCalled(); |
| 124 | + expect(drafts.setPrompt).not.toHaveBeenCalled(); |
| 125 | + }); |
| 126 | + |
| 127 | + it("reports a missing destination without mutating composer state", async () => { |
| 128 | + vi.mocked(readThreadDetail).mockReturnValue(sourceThread([message("user", "Continue")])); |
| 129 | + |
| 130 | + const result = await continueThreadInNewDraft({ |
| 131 | + threadRef, |
| 132 | + createDraft: async () => null, |
| 133 | + }); |
| 134 | + |
| 135 | + expect(result).toMatchObject({ ok: false }); |
| 136 | + expect(drafts.setPrompt).not.toHaveBeenCalled(); |
| 137 | + }); |
| 138 | +}); |
0 commit comments