Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[test]
preload = ["./packages/opencode/src/tests/preload.ts"]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "cd packages/client && bun run build && cd ../opencode && bun run build",
"typecheck": "cd packages/client && bun run build && bun run typecheck && cd ../opencode && bun run typecheck",
"test": "bun run build && bun test packages",
"test:hermetic": "bun run build && XDG_RUNTIME_DIR=/nonexistent CLAUSTRUM_SUBC_CONNECTION=/nonexistent/x.json bun test packages"
"test:hermetic": "bun run build && XDG_RUNTIME_DIR=/nonexistent CLAUSTRUM_SUBC_CONNECTION=/nonexistent/x.json CLAUSTRUM_CUSTODY_LOG=off bun test packages"
},
"devDependencies": {
"@types/bun": "1.3.14",
Expand Down
2 changes: 2 additions & 0 deletions packages/opencode/bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[test]
preload = ["./src/tests/preload.ts"]
8 changes: 8 additions & 0 deletions packages/opencode/src/tests/preload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

if (process.env.CLAUSTRUM_CUSTODY_LOG === undefined) {
process.env.CLAUSTRUM_CUSTODY_LOG = "off";
}
process.env.XDG_STATE_HOME = mkdtempSync(join(tmpdir(), "claustrum-opencode-test-state-"));
71 changes: 71 additions & 0 deletions packages/opencode/src/tests/sink-isolation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { afterEach, expect, test } from "bun:test";
import { existsSync, readFileSync, statSync } from "node:fs";
import { join } from "node:path";

import type { ClaustrumClient } from "@cortexkit/claustrum-client";

import { createOpencodeClaustrumPlugin } from "../plugin";
import { sentinel, tombstoneFor } from "../tombstone";

const PROVIDER = "deepseek";
const HANDLE = `ckh_${"a".repeat(43)}`;
const liveLogPath = () => join(process.env.HOME ?? ".", ".local", "state", "cortexkit", "opencode-plugin", "custody.jsonl");
const savedEnv = new Map<string, string | undefined>();

function useEnv(key: string, value: string | undefined) {
if (!savedEnv.has(key)) savedEnv.set(key, process.env[key]);
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}

afterEach(() => {
for (const [key, value] of savedEnv) {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
savedEnv.clear();
});

test("a plugin serve without an injected logger does not write the live custody log", async () => {
useEnv("CLAUSTRUM_CUSTODY_LOG", undefined);
const path = liveLogPath();
const beforeExists = existsSync(path);
const beforeContent = beforeExists ? readFileSync(path, "utf8") : "";
const beforeMtimeMs = beforeExists ? statSync(path).mtimeMs : undefined;

const plugin = createOpencodeClaustrumPlugin({
handleReader: async () => ({
version: 1,
providers: [{
provider: PROVIDER,
shape: "api",
serve: "opencode-claustrum",
accounts: [{ label: "main", handle: HANDLE, credential_id: `apikey:${PROVIDER}:main` }],
}],
}),
authReader: async () => ({ [PROVIDER]: tombstoneFor("api", PROVIDER) }),
detect: async () => ({ status: "available" as const, schema: 1, wireVersion: 1, endpoints: [] }),
clientFactory: async () => ({
getCredential: async () => ({ material: "served-material", recordVersion: 1, expiresAtMs: null }),
reportAuthFailure: async () => {},
} as unknown as ClaustrumClient),
fetch: (async () => new Response("upstream", { status: 200 })) as unknown as typeof globalThis.fetch,
handleVersionReader: async () => "stable",
});
const hooks = await plugin({} as never);
const config: { provider: Record<string, { options?: Record<string, unknown> }> } = { provider: {} };

if (!hooks.config) throw new Error("plugin did not provide config hook");
await hooks.config(config);
const serve = config.provider[PROVIDER]?.options?.fetch;
expect(typeof serve).toBe("function");
await (serve as (input: string, init?: RequestInit) => Promise<Response>)
("https://upstream.example/v1/chat", { headers: { Authorization: `Bearer ${sentinel(PROVIDER)}` } });

const afterExists = existsSync(path);
expect(afterExists).toBe(beforeExists);
if (beforeExists) {
expect(readFileSync(path, "utf8")).toBe(beforeContent);
if (beforeMtimeMs !== undefined) expect(statSync(path).mtimeMs).toBe(beforeMtimeMs);
}
});
Loading