|
| 1 | +import { |
| 2 | + CommandId, |
| 3 | + EventId, |
| 4 | + MessageId, |
| 5 | + ProjectId, |
| 6 | + ProviderInstanceId, |
| 7 | + ThreadId, |
| 8 | + type OrchestrationCommand, |
| 9 | + type OrchestrationEvent, |
| 10 | +} from "@t3tools/contracts"; |
| 11 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 12 | +import { expect, it } from "@effect/vitest"; |
| 13 | +import * as Effect from "effect/Effect"; |
| 14 | + |
| 15 | +import { decideOrchestrationCommand } from "./decider.ts"; |
| 16 | +import { createEmptyReadModel, projectEvent } from "./projector.ts"; |
| 17 | + |
| 18 | +const NOW = "2026-01-01T00:00:00.000Z"; |
| 19 | +const PROJECT_ID = ProjectId.make("project-history-import"); |
| 20 | +const THREAD_ID = ThreadId.make("thread-history-import"); |
| 21 | + |
| 22 | +const seedReadModel = projectEvent(createEmptyReadModel(NOW), { |
| 23 | + sequence: 1, |
| 24 | + eventId: EventId.make("evt-project-history-import"), |
| 25 | + aggregateKind: "project", |
| 26 | + aggregateId: PROJECT_ID, |
| 27 | + type: "project.created", |
| 28 | + occurredAt: NOW, |
| 29 | + commandId: CommandId.make("cmd-project-history-import"), |
| 30 | + causationEventId: null, |
| 31 | + correlationId: CommandId.make("cmd-project-history-import"), |
| 32 | + metadata: {}, |
| 33 | + payload: { |
| 34 | + projectId: PROJECT_ID, |
| 35 | + title: "History Import", |
| 36 | + workspaceRoot: "/tmp/history-import", |
| 37 | + defaultModelSelection: null, |
| 38 | + scripts: [], |
| 39 | + createdAt: NOW, |
| 40 | + updatedAt: NOW, |
| 41 | + }, |
| 42 | +}); |
| 43 | + |
| 44 | +it.layer(NodeServices.layer)("history import decider", (it) => { |
| 45 | + it.effect("creates a non-running thread and projects its text snapshot in order", () => |
| 46 | + Effect.gen(function* () { |
| 47 | + const readModel = yield* seedReadModel; |
| 48 | + const command = { |
| 49 | + type: "thread.history.import", |
| 50 | + commandId: CommandId.make("cmd-history-import"), |
| 51 | + threadId: THREAD_ID, |
| 52 | + projectId: PROJECT_ID, |
| 53 | + title: "Codex conversation", |
| 54 | + modelSelection: { |
| 55 | + instanceId: ProviderInstanceId.make("codex"), |
| 56 | + model: "gpt-5-codex", |
| 57 | + }, |
| 58 | + runtimeMode: "full-access", |
| 59 | + interactionMode: "default", |
| 60 | + createdAt: NOW, |
| 61 | + messages: [ |
| 62 | + { |
| 63 | + messageId: MessageId.make("message-history-user"), |
| 64 | + role: "user", |
| 65 | + text: "Please inspect this project.", |
| 66 | + turnId: null, |
| 67 | + createdAt: "2026-01-01T00:01:00.000Z", |
| 68 | + }, |
| 69 | + { |
| 70 | + messageId: MessageId.make("message-history-assistant"), |
| 71 | + role: "assistant", |
| 72 | + text: "I found the relevant files.", |
| 73 | + turnId: null, |
| 74 | + createdAt: "2026-01-01T00:02:00.000Z", |
| 75 | + }, |
| 76 | + ], |
| 77 | + } satisfies OrchestrationCommand; |
| 78 | + |
| 79 | + const result = yield* decideOrchestrationCommand({ command, readModel }); |
| 80 | + const events = Array.isArray(result) ? result : [result]; |
| 81 | + |
| 82 | + expect(events.map((event) => event.type)).toEqual([ |
| 83 | + "thread.created", |
| 84 | + "thread.message-sent", |
| 85 | + "thread.message-sent", |
| 86 | + ]); |
| 87 | + const created = events[0]; |
| 88 | + expect(created?.type).toBe("thread.created"); |
| 89 | + if (created?.type === "thread.created") { |
| 90 | + expect(created.payload.branch).toBeNull(); |
| 91 | + expect(created.payload.worktreePath).toBeNull(); |
| 92 | + } |
| 93 | + const messages = events.filter( |
| 94 | + (event): event is Extract<OrchestrationEvent, { type: "thread.message-sent" }> => |
| 95 | + event.type === "thread.message-sent", |
| 96 | + ); |
| 97 | + expect(messages.map((event) => event.payload.text)).toEqual([ |
| 98 | + "Please inspect this project.", |
| 99 | + "I found the relevant files.", |
| 100 | + ]); |
| 101 | + expect(messages.every((event) => event.payload.streaming === false)).toBe(true); |
| 102 | + |
| 103 | + let projected = readModel; |
| 104 | + for (const [index, event] of events.entries()) { |
| 105 | + projected = yield* projectEvent(projected, { ...event, sequence: index + 2 }); |
| 106 | + } |
| 107 | + const thread = projected.threads.find((entry) => entry.id === THREAD_ID); |
| 108 | + expect(thread?.session).toBeNull(); |
| 109 | + expect(thread?.messages.map((message) => [message.role, message.text])).toEqual([ |
| 110 | + ["user", "Please inspect this project."], |
| 111 | + ["assistant", "I found the relevant files."], |
| 112 | + ]); |
| 113 | + }), |
| 114 | + ); |
| 115 | +}); |
0 commit comments