1import { assert, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Exit from "effect/Exit";
4import * as Schema from "effect/Schema";
5
6import {
7 ChatAttachment,
8 PROVIDER_SEND_TURN_MAX_FILE_BYTES,
9 SnapShotAccessibility,
10} from "./chatAttachment.ts";
11
12const decodeAttachment = Schema.decodeUnknownEffect(ChatAttachment);
13const decodeSnapShotAccessibility = Schema.decodeUnknownEffect(SnapShotAccessibility);
14
15it.effect("tolerates attachment types from newer builds", () =>
16 Effect.gen(function* () {
17 const attachment = yield* decodeAttachment({
18 type: "somethingnew",
19 id: "thread-1-00000000-0000-4000-8000-000000000003-glb",
20 name: "scene.glb",
21 mimeType: "model/gltf-binary",
22 sizeBytes: 12,
23 });
24 assert.strictEqual(attachment.type, "somethingnew");
25 }),
26);
27
28it.effect("rejects malformed known attachment types instead of tolerating them", () =>
29 Effect.gen(function* () {
30 const base = {
31 id: "thread-1-00000000-0000-4000-8000-000000000003-pdf",
32 name: "report.pdf",
33 mimeType: "application/pdf",
34 };
35 const oversizedFile = yield* Effect.exit(
36 decodeAttachment({ ...base, type: "file", sizeBytes: PROVIDER_SEND_TURN_MAX_FILE_BYTES + 1 }),
37 );
38 assert.strictEqual(Exit.isFailure(oversizedFile), true);
39 const badMimeImage = yield* Effect.exit(
40 decodeAttachment({ ...base, type: "image", mimeType: "application/pdf", sizeBytes: 12 }),
41 );
42 assert.strictEqual(Exit.isFailure(badMimeImage), true);
43 }),
44);
45
46it.effect("rejects accessibility trees above the serialized payload limit", () =>
47 Effect.gen(function* () {
48 const result = yield* Effect.exit(
49 decodeSnapShotAccessibility({
50 format: "element-tree",
51 coordinateSpace: "captured-image",
52 imageSize: { width: 800, height: 600 },
53 truncated: false,
54 root: {
55 role: "window",
56 bounds: { x: 0, y: 0, width: 800, height: 600 },
57 children: Array.from({ length: 10 }, () => ({
58 role: "text",
59 value: "x".repeat(8_000),
60 bounds: null,
61 children: [],
62 })),
63 },
64 }),
65 );
66
67 assert.strictEqual(Exit.isFailure(result), true);
68 }),
69);