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

AgentSessionImporter.test.ts

166 lines5.9 KB

1import { expect, it } from "@effect/vitest";
2import {
3 ProjectId,
4 ProviderInstanceId,
5 ThreadId,
6 type OrchestrationV2DomainEvent,
7} from "@t3tools/contracts";
8import * as Effect from "effect/Effect";
9import * as Layer from "effect/Layer";
10import * as Option from "effect/Option";
11import * as Stream from "effect/Stream";
12​
13import { EventSinkV2 } from "../orchestration-v2/EventSink.ts";
14import { layer as idAllocatorLayer } from "../orchestration-v2/IdAllocator.ts";
15import { OrchestratorProjectionError, OrchestratorV2 } from "../orchestration-v2/Orchestrator.ts";
16import { ProviderSessionRuntimeRepository } from "../persistence/ProviderSessionRuntime.ts";
17import { AgentSessionImporter, layer } from "./AgentSessionImporter.ts";
18import * as AgentSessionScanner from "./AgentSessionScanner.ts";
19import { ProjectService } from "./ProjectService.ts";
20​
21const projectId = ProjectId.make("agent-session-import-project");
22for (const { source, instanceId, providerSessionId, model, resumeCursor } of [
23 {
24 source: "codex" as const,
25 instanceId: "codex",
26 providerSessionId: "native-codex-thread",
27 model: "gpt-5.4",
28 resumeCursor: { threadId: "native-codex-thread" },
29 },
30 {
31 source: "muse" as const,
32 instanceId: "muse-work",
33 providerSessionId: "48af9116-9220-4a13-94f4-d0593510582a",
34 model: "muse-spark-1.3-contributor",
35 resumeCursor: { sessionId: "48af9116-9220-4a13-94f4-d0593510582a" },
36 },
37]) {
38 const providerInstanceId = ProviderInstanceId.make(instanceId);
39 const threadId = ThreadId.make(`import:${providerInstanceId}:${providerSessionId}`);
40​
41 it.effect(
42 `${source}: imports messages once and preserves the provider native resume binding`,
43 () => {
44 const writes: Array<ReadonlyArray<OrchestrationV2DomainEvent>> = [];
45 const upserts: Array<unknown> = [];
46 const recorded: Array<unknown> = [];
47 let imported = false;
48 const scanner = AgentSessionScanner.AgentSessionScanner.of({
49 scan: Effect.die("unused"),
50 recentThreads: () =>
51 Stream.succeed({
52 _tag: "Importable",
53 source: {
54 provider: source,
55 providerInstanceId,
56 providerSessionId,
57 filePath: "/tmp/native-codex-thread.jsonl",
58 size: 100,
59 mtimeMs: 2,
60 device: 3,
61 inode: 4,
62 birthtimeMs: 1,
63 },
64 thread: {
65 source,
66 providerInstanceId,
67 providerSessionId,
68 title: "Imported thread",
69 model,
70 createdAt: "2026-09-01T10:00:00.000Z",
71 updatedAt: "2026-09-01T10:01:00.000Z",
72 messages: [
73 { role: "user", text: "Fix it", createdAt: "2026-09-01T10:00:00.000Z" },
74 { role: "assistant", text: "Fixed", createdAt: "2026-09-01T10:01:00.000Z" },
75 ],
76 },
77 }),
78 });
79 const testLayer = layer.pipe(
80 Layer.provide(
81 Layer.mergeAll(
82 Layer.succeed(AgentSessionScanner.AgentSessionScanner, scanner),
83 Layer.mock(ProjectService)({
84 getById: () =>
85 Effect.succeed(
86 Option.some({ id: projectId, workspaceRoot: "/workspace/project" } as never),
87 ),
88 }),
89 Layer.mock(OrchestratorV2)({
90 getThreadProjection: () =>
91 imported
92 ? Effect.succeed({
93 thread: { id: threadId, projectId, historyOrigin: "v1_import" },
94 } as never)
95 : Effect.fail(new OrchestratorProjectionError({ threadId })),
96 }),
97 Layer.mock(EventSinkV2)({
98 write: (input) =>
99 Effect.sync(() => {
100 writes.push(input.events);
101 imported = true;
102 return [];
103 }),
104 }),
105 Layer.mock(ProviderSessionRuntimeRepository)({
106 list: () => Effect.succeed([]),
107 upsert: (input) => Effect.sync(() => void upserts.push(input)),
108 recordImportedTranscript: (input) => Effect.sync(() => void recorded.push(input)),
109 }),
110 idAllocatorLayer,
111 ),
112 ),
113 );
114​
115 return Effect.gen(function* () {
116 const importer = yield* AgentSessionImporter;
117 expect(yield* importer.importRecentAgentThreads({ projectId })).toEqual({
118 importedCount: 1,
119 skippedCount: 0,
120 });
121 expect(yield* importer.importRecentAgentThreads({ projectId })).toEqual({
122 importedCount: 1,
123 skippedCount: 0,
124 });
125​
126 expect(writes).toHaveLength(1);
127 expect(writes[0]?.map((event) => event.type)).toEqual([
128 "thread.created",
129 "message.updated",
130 "turn-item.updated",
131 "message.updated",
132 "turn-item.updated",
133 "provider-thread.updated",
134 ]);
135 const created = writes[0]?.find((event) => event.type === "thread.created");
136 const providerThread = writes[0]?.find((event) => event.type === "provider-thread.updated");
137 expect(created?.payload).toMatchObject({
138 id: threadId,
139 activeProviderThreadId: providerThread?.payload.id,
140 historyOrigin: "v1_import",
141 });
142 expect(providerThread?.payload).toMatchObject({
143 appThreadId: threadId,
144 nativeThreadRef: {
145 driver: source,
146 nativeId: providerSessionId,
147 strength: "strong",
148 },
149 });
150 expect(
151 writes[0]
152 ?.filter((event) => event.type === "message.updated")
153 .map((event) => event.payload.text),
154 ).toEqual(["Fix it", "Fixed"]);
155 expect(upserts).toEqual([
156 expect.objectContaining({
157 threadId,
158 providerInstanceId,
159 resumeCursor,
160 }),
161 ]);
162 expect(recorded).toHaveLength(2);
163 }).pipe(Effect.provide(testLayer));
164 },
165 );