Files
t3code/apps/server/src/attachmentStore.test.ts
T
Julius Marminge f456ee9630 Complete orchestration V2 frontend cutover
- Migrate web and mobile state, commands, and persistence to V2 projections
- Render conversations from authoritative visible turn items
- Harden runtime policy, launch handling, attachments, and cache invalidation
2026-08-15 16:49:33 +02:00

92 lines
3.1 KiB
TypeScript

// @effect-diagnostics nodeBuiltinImport:off
import * as NodeFS from "node:fs";
import * as NodeOS from "node:os";
import * as NodePath from "node:path";
import { describe, expect, it } from "vite-plus/test";
import {
createAttachmentId,
createDeterministicAttachmentId,
parseThreadSegmentFromAttachmentId,
resolveAttachmentPathById,
} from "./attachmentStore.ts";
describe("attachmentStore", () => {
it("derives stable attachment ids for idempotent message retries", () => {
const first = createDeterministicAttachmentId("thread-1", "message-1:0");
const retry = createDeterministicAttachmentId("thread-1", "message-1:0");
const next = createDeterministicAttachmentId("thread-1", "message-1:1");
expect(first).toBe(retry);
expect(next).not.toBe(first);
expect(first && parseThreadSegmentFromAttachmentId(first)).toBe("thread-1");
});
it("sanitizes thread ids when creating attachment ids", () => {
const attachmentId = createAttachmentId("thread.folder/unsafe space");
expect(attachmentId).toBeTruthy();
if (!attachmentId) {
return;
}
const threadSegment = parseThreadSegmentFromAttachmentId(attachmentId);
expect(threadSegment).toBeTruthy();
expect(threadSegment).toMatch(/^[a-z0-9_-]+$/i);
expect(threadSegment).not.toContain(".");
expect(threadSegment).not.toContain("%");
expect(threadSegment).not.toContain("/");
});
it("parses exact thread segments from attachment ids without prefix collisions", () => {
const fooId = "foo-00000000-0000-4000-8000-000000000001";
const fooBarId = "foo-bar-00000000-0000-4000-8000-000000000002";
expect(parseThreadSegmentFromAttachmentId(fooId)).toBe("foo");
expect(parseThreadSegmentFromAttachmentId(fooBarId)).toBe("foo-bar");
});
it("normalizes created thread segments to lowercase", () => {
const attachmentId = createAttachmentId("Thread.Foo");
expect(attachmentId).toBeTruthy();
if (!attachmentId) {
return;
}
expect(parseThreadSegmentFromAttachmentId(attachmentId)).toBe("thread-foo");
});
it("resolves attachment path by id using the extension that exists on disk", () => {
const attachmentsDir = NodeFS.mkdtempSync(
NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"),
);
try {
const attachmentId = "thread-1-attachment";
const pngPath = NodePath.join(attachmentsDir, `${attachmentId}.png`);
NodeFS.writeFileSync(pngPath, Buffer.from("hello"));
const resolved = resolveAttachmentPathById({
attachmentsDir,
attachmentId,
});
expect(resolved).toBe(pngPath);
} finally {
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
}
});
it("returns null when no attachment file exists for the id", () => {
const attachmentsDir = NodeFS.mkdtempSync(
NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"),
);
try {
const resolved = resolveAttachmentPathById({
attachmentsDir,
attachmentId: "thread-1-missing",
});
expect(resolved).toBeNull();
} finally {
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
}
});
});