|
| 1 | +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 2 | +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 4 | + |
| 5 | +import { |
| 6 | + composerTargetKey, |
| 7 | + type ComposerImageAttachment, |
| 8 | + useComposerDraftStore, |
| 9 | +} from "../../composerDraftStore"; |
| 10 | +import { imageContextReference } from "../../lib/composerContextRecords"; |
| 11 | +import { ensureInlineContextReferences } from "../../lib/composerContextReferences"; |
| 12 | +import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences"; |
| 13 | + |
| 14 | +afterEach(() => { |
| 15 | + useComposerDraftStore.setState({ draftsByThreadKey: {} }); |
| 16 | +}); |
| 17 | + |
| 18 | +describe("async attachment references", () => { |
| 19 | + it.each(["thread", "environment"])( |
| 20 | + "keeps a deferred image in its original draft after a %s switch", |
| 21 | + async (switchKind) => { |
| 22 | + const targetA = scopeThreadRef(EnvironmentId.make("env-A"), ThreadId.make("thread-A")); |
| 23 | + const targetB = scopeThreadRef( |
| 24 | + EnvironmentId.make(switchKind === "environment" ? "env-B" : "env-A"), |
| 25 | + ThreadId.make(switchKind === "environment" ? "thread-A" : "thread-B"), |
| 26 | + ); |
| 27 | + const store = useComposerDraftStore.getState(); |
| 28 | + store.setPrompt(targetA, "Draft A original"); |
| 29 | + store.setPrompt(targetB, "Draft B original"); |
| 30 | + let liveTargetKey = composerTargetKey(targetA); |
| 31 | + const promptRef = { current: "Draft A original" }; |
| 32 | + const editor = { text: promptRef.current, caret: 4 }; |
| 33 | + const insertReferences = vi.fn(() => { |
| 34 | + const next = ensureInlineContextReferences(promptRef.current, [ |
| 35 | + imageContextReference(image), |
| 36 | + ]); |
| 37 | + store.setPrompt(targetA, next); |
| 38 | + promptRef.current = next; |
| 39 | + editor.text = next; |
| 40 | + editor.caret = next.length; |
| 41 | + return true; |
| 42 | + }); |
| 43 | + const image: ComposerImageAttachment = { |
| 44 | + type: "image", |
| 45 | + id: "image-1", |
| 46 | + name: "picture.png", |
| 47 | + mimeType: "image/png", |
| 48 | + sizeBytes: 3, |
| 49 | + file: new File(["png"], "picture.png", { type: "image/png" }), |
| 50 | + previewUrl: "blob:picture", |
| 51 | + }; |
| 52 | + let finishCompression!: (image: ComposerImageAttachment) => void; |
| 53 | + const compression = new Promise<ComposerImageAttachment>((resolve) => { |
| 54 | + finishCompression = resolve; |
| 55 | + }); |
| 56 | + const pending = compression.then((compressed) => { |
| 57 | + const accepted = store.addImages(targetA, [compressed]); |
| 58 | + expect(accepted).toEqual([image.id]); |
| 59 | + return insertAttachmentReferencesForDraft({ |
| 60 | + target: targetA, |
| 61 | + liveAttachmentTargetKey: liveTargetKey, |
| 62 | + references: [imageContextReference(compressed)], |
| 63 | + insertReferences, |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + liveTargetKey = composerTargetKey(targetB); |
| 68 | + promptRef.current = "Draft B original"; |
| 69 | + editor.text = promptRef.current; |
| 70 | + // Use the latest stored A prompt, including edits from elsewhere during compression. |
| 71 | + store.setPrompt(targetA, "Draft A original updated"); |
| 72 | + finishCompression(image); |
| 73 | + expect(await pending).toBe(true); |
| 74 | + |
| 75 | + expect(store.getComposerDraft(targetA)?.prompt).toBe( |
| 76 | + ensureInlineContextReferences("Draft A original updated", [imageContextReference(image)]), |
| 77 | + ); |
| 78 | + expect(store.getComposerDraft(targetA)?.images).toEqual([image]); |
| 79 | + expect(store.getComposerDraft(targetB)?.prompt).toBe("Draft B original"); |
| 80 | + expect(store.getComposerDraft(targetB)?.images).toEqual([]); |
| 81 | + expect(promptRef.current).toBe("Draft B original"); |
| 82 | + expect(editor).toEqual({ text: "Draft B original", caret: 4 }); |
| 83 | + expect(insertReferences).not.toHaveBeenCalled(); |
| 84 | + }, |
| 85 | + ); |
| 86 | +}); |
0 commit comments