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

ThreadMessageIntake.test.ts

79 lines2.8 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodePath from "node:path";
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import { expect, it } from "@effect/vitest";
6import {
7 ChatAttachmentId,
8 CommandId,
9 RuntimeRequestId,
10 ThreadId,
11 type OrchestrationV2Command,
12} from "@t3tools/contracts";
13import * as Effect from "effect/Effect";
14import * as Layer from "effect/Layer";
15import { createPendingAttachmentId, resolveAttachmentPath } from "../attachmentStore.ts";
16import * as ServerConfig from "../config.ts";
17import { OrchestratorDispatchError } from "./Orchestrator.ts";
18import { ThreadManagementService } from "./ThreadManagementService.ts";
19import { dispatchCommand } from "./ThreadMessageIntake.ts";
20​
21it.effect("claims question uploads and passes readable paths through the V2 request command", () =>
22 Effect.gen(function* () {
23 const config = yield* ServerConfig.ServerConfig;
24 const id = ChatAttachmentId.make(createPendingAttachmentId()!);
25 NodeFS.writeFileSync(
26 NodePath.join(config.attachmentsDir, `${id}.png`),
27 new Uint8Array([1, 2, 3]),
28 );
29 const captured: OrchestrationV2Command[] = [];
30 yield* dispatchCommand({
31 type: "runtime-request.respond",
32 commandId: CommandId.make("answer-1"),
33 threadId: ThreadId.make("thread-answer"),
34 requestId: RuntimeRequestId.make("request-1"),
35 answers: { q: ["Selected option"] },
36 attachmentsByQuestionId: {
37 q: [{ type: "image", id, name: "screen.png", mimeType: "image/png", sizeBytes: 3 }],
38 },
39 }).pipe(
40 Effect.provide(
41 Layer.mock(ThreadManagementService)({
42 dispatch: (command) => {
43 captured.push(command);
44 return Effect.fail(
45 new OrchestratorDispatchError({
46 commandId: command.commandId,
47 commandType: command.type,
48 cause: "Stop after intake",
49 }),
50 );
51 },
52 }),
53 ),
54 Effect.result,
55 );
56 expect(captured).toHaveLength(1);
57 const command = captured[0]!;
58 expect(command.type).toBe("runtime-request.respond");
59 if (command.type !== "runtime-request.respond") return;
60 const attachment = command.attachmentsByQuestionId?.q?.[0];
61 expect(attachment).toBeDefined();
62 const path = resolveAttachmentPath({
63 attachmentsDir: config.attachmentsDir,
64 attachment: attachment!,
65 });
66 expect(path).not.toBeNull();
67 expect(NodeFS.readFileSync(path!)).toEqual(Buffer.from([1, 2, 3]));
68 expect(command.answers?.q).toEqual([
69 "Selected option",
70 `Attached image "screen.png": "${path}"`,
71 ]);
72 }).pipe(
73 Effect.provide(
74 ServerConfig.layerTest(process.cwd(), { prefix: "t3-question-intake-" }).pipe(
75 Layer.provideMerge(NodeServices.layer),
76 ),
77 ),
78 ),
79);