|
| 1 | +import { threadContextRecord } from "@t3tools/shared/composerContextReferences"; |
1 | 2 | import { useEffect } from "react"; |
2 | | -import { type ComposerContextRecord, ComposerContextId, EnvironmentId } from "@t3tools/contracts"; |
| 3 | +import { |
| 4 | + type ComposerContextRecord, |
| 5 | + ComposerContextId, |
| 6 | + EnvironmentId, |
| 7 | + ThreadId, |
| 8 | +} from "@t3tools/contracts"; |
3 | 9 | import { expect, it, vi } from "vite-plus/test"; |
4 | 10 |
|
5 | 11 | import { act, render, stubGlobal } from "../../../test/render"; |
@@ -186,3 +192,64 @@ it.each([ |
186 | 192 | : [], |
187 | 193 | ); |
188 | 194 | }); |
| 195 | + |
| 196 | +it.each([false, true])( |
| 197 | + "preserves thread identity when pasting a colliding context (other environment: %s)", |
| 198 | + async (otherEnvironment) => { |
| 199 | + const target = DraftId.make(`thread-collision-${otherEnvironment}`); |
| 200 | + const store = useComposerDraftStore.getState(); |
| 201 | + using _draft = { [Symbol.dispose]: () => store.clearComposerContent(target) }; |
| 202 | + const environmentId = EnvironmentId.make("thread-source"); |
| 203 | + const otherEnvironmentId = EnvironmentId.make("other-thread-source"); |
| 204 | + const first = threadContextRecord( |
| 205 | + { environmentId, threadId: ThreadId.make("same-id") }, |
| 206 | + "First", |
| 207 | + ); |
| 208 | + const pasted = otherEnvironment |
| 209 | + ? threadContextRecord( |
| 210 | + { environmentId: otherEnvironmentId, threadId: first.threadId }, |
| 211 | + "Second", |
| 212 | + ) |
| 213 | + : first; |
| 214 | + store.addThreadContexts(target, [first], { appendReference: false }); |
| 215 | + let importer!: Importer; |
| 216 | + await using _screen = await render( |
| 217 | + <Harness |
| 218 | + input={{ |
| 219 | + environmentId, |
| 220 | + linkedEnvironments: new Map([[otherEnvironmentId, "Other machine"]]), |
| 221 | + activeThread: undefined, |
| 222 | + activeThreadId: null, |
| 223 | + composerDraftTarget: target, |
| 224 | + attachmentDraftTarget: target, |
| 225 | + attachmentTargetKey: target, |
| 226 | + attachmentTargetKeyRef: { current: target }, |
| 227 | + questionAttachmentTarget: null, |
| 228 | + pendingUserInputs: [], |
| 229 | + composerContextRecords: new Map([[first.contextId, { kind: "thread", record: first }]]), |
| 230 | + composerImages: [], |
| 231 | + composerFilesRef: { current: [] }, |
| 232 | + pendingThreadImportsRef: { current: new Map() }, |
| 233 | + promptRef: { current: "" }, |
| 234 | + addComposerFilesToDraft: () => [], |
| 235 | + }} |
| 236 | + ready={(value) => { |
| 237 | + importer = value; |
| 238 | + }} |
| 239 | + />, |
| 240 | + ); |
| 241 | + let importedId: string | undefined; |
| 242 | + await act(() => { |
| 243 | + importedId = importer |
| 244 | + .importContextRecords([pasted], pasted.environmentId) |
| 245 | + .get(pasted.contextId); |
| 246 | + }); |
| 247 | + const records = store.getComposerDraft(target)!.threadContexts; |
| 248 | + expect(records.find((record) => record.contextId === importedId)).toEqual({ |
| 249 | + ...pasted, |
| 250 | + contextId: importedId, |
| 251 | + }); |
| 252 | + expect(records).toHaveLength(otherEnvironment ? 2 : 1); |
| 253 | + expect(records[0]).toEqual(first); |
| 254 | + }, |
| 255 | +); |
0 commit comments