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

OpenCodeRuntime.cliParsers.test.ts

101 lines3.5 KB

1import * as NodeAssert from "node:assert/strict";
2​
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { describe, it } from "@effect/vitest";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Path from "effect/Path";
8​
9import * as OpenCodeRuntime from "./OpenCodeRuntime.ts";
10​
11describe("parseServerUrlFromOutput", () => {
12 it.each([
13 ["opencode server listening on http://127.0.0.1:4096", null],
14 ["opencode server listening on http://127.0.0.1:40961\n", "http://127.0.0.1:40961"],
15 ["opencode server listening on http://127.0.0.1:40961\r\n", "http://127.0.0.1:40961"],
16 ])("reads only a newline-terminated ready line: %j", (output, expected) => {
17 NodeAssert.equal(OpenCodeRuntime.parseServerUrlFromOutput(output), expected);
18 });
19});
20​
21it.layer(NodeServices.layer)("toOpenCodeFileParts", (it) => {
22 const attachment = (mimeType: string, sizeBytes = 12) => ({
23 type: "file" as const,
24 id: "thread-1-00000000-0000-4000-8000-000000000001-bin",
25 name: "attachment",
26 mimeType,
27 sizeBytes,
28 });
29​
30 it.effect("sends supported images, text, and PDFs natively and skips what models reject", () =>
31 Effect.gen(function* () {
32 const fileSystem = yield* FileSystem.FileSystem;
33 const parts = yield* OpenCodeRuntime.toOpenCodeFileParts({
34 attachments: [
35 attachment("application/pdf"),
36 attachment("text/plain"),
37 attachment("image/png"),
38 // ZIP file parts fail Anthropic before turn start; send only the prompt's path line.
39 attachment("application/zip"),
40 // OpenCode inlines only text/plain; other text MIME parts fail Anthropic/OpenAI.
41 attachment("text/html"),
42 attachment("text/markdown"),
43 attachment("application/octet-stream"),
44​
45 attachment("image/bmp"),
46 attachment("image/svg+xml"),
47​
48 attachment("application/pdf", 21 * 1024 * 1024),
49 ],
50 resolveAttachmentPath: () => "/tmp/attachment",
51 fileSystem,
52 });
53​
54 NodeAssert.deepEqual(
55 parts.map((part) => part.mime),
56 ["application/pdf", "text/plain", "image/png"],
57 );
58 }),
59 );
60​
61 it.effect("labels an image with the type its bytes have, not the declared one", () =>
62 Effect.gen(function* () {
63 const fileSystem = yield* FileSystem.FileSystem;
64 const path = yield* Path.Path;
65 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-opencode-attachment-" });
66 const filePath = path.join(dir, "screenshot.jpg");
67 yield* fileSystem.writeFile(
68 filePath,
69 new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
70 );
71 const parts = yield* OpenCodeRuntime.toOpenCodeFileParts({
72 attachments: [{ ...attachment("image/jpeg"), type: "image" as const }],
73 resolveAttachmentPath: () => filePath,
74 fileSystem,
75 });
76​
77 NodeAssert.deepEqual(
78 parts.map((part) => part.mime),
79 ["image/png"],
80 );
81 }).pipe(Effect.scoped),
82 );
83​
84 it.effect("keeps folded clipboard text on the lazy path fallback", () =>
85 Effect.gen(function* () {
86 const fileSystem = yield* FileSystem.FileSystem;
87 const parts = yield* OpenCodeRuntime.toOpenCodeFileParts({
88 attachments: [
89 {
90 ...attachment("text/plain"),
91 source: { _tag: "pasted-text" as const },
92 },
93 ],
94 resolveAttachmentPath: () => "/tmp/pasted-text.txt",
95 fileSystem,
96 });
97​
98 NodeAssert.deepEqual(parts, []);
99 }),
100 );
101});