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

attachmentStore.test.ts

91 lines3.1 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5​
6import { describe, expect, it } from "vite-plus/test";
7​
8import {
9 createAttachmentId,
10 createDeterministicAttachmentId,
11 parseThreadSegmentFromAttachmentId,
12 resolveAttachmentPathById,
13} from "./attachmentStore.ts";
14​
15describe("attachmentStore", () => {
16 it("derives stable attachment ids for idempotent message retries", () => {
17 const first = createDeterministicAttachmentId("thread-1", "message-1:0");
18 const retry = createDeterministicAttachmentId("thread-1", "message-1:0");
19 const next = createDeterministicAttachmentId("thread-1", "message-1:1");
20​
21 expect(first).toBe(retry);
22 expect(next).not.toBe(first);
23 expect(first && parseThreadSegmentFromAttachmentId(first)).toBe("thread-1");
24 });
25​
26 it("sanitizes thread ids when creating attachment ids", () => {
27 const attachmentId = createAttachmentId("thread.folder/unsafe space");
28 expect(attachmentId).toBeTruthy();
29 if (!attachmentId) {
30 return;
31 }
32​
33 const threadSegment = parseThreadSegmentFromAttachmentId(attachmentId);
34 expect(threadSegment).toBeTruthy();
35 expect(threadSegment).toMatch(/^[a-z0-9_-]+$/i);
36 expect(threadSegment).not.toContain(".");
37 expect(threadSegment).not.toContain("%");
38 expect(threadSegment).not.toContain("/");
39 });
40​
41 it("parses exact thread segments from attachment ids without prefix collisions", () => {
42 const fooId = "foo-00000000-0000-4000-8000-000000000001";
43 const fooBarId = "foo-bar-00000000-0000-4000-8000-000000000002";
44​
45 expect(parseThreadSegmentFromAttachmentId(fooId)).toBe("foo");
46 expect(parseThreadSegmentFromAttachmentId(fooBarId)).toBe("foo-bar");
47 });
48​
49 it("normalizes created thread segments to lowercase", () => {
50 const attachmentId = createAttachmentId("Thread.Foo");
51 expect(attachmentId).toBeTruthy();
52 if (!attachmentId) {
53 return;
54 }
55 expect(parseThreadSegmentFromAttachmentId(attachmentId)).toBe("thread-foo");
56 });
57​
58 it("resolves attachment path by id using the extension that exists on disk", () => {
59 const attachmentsDir = NodeFS.mkdtempSync(
60 NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"),
61 );
62 try {
63 const attachmentId = "thread-1-attachment";
64 const pngPath = NodePath.join(attachmentsDir, `${attachmentId}.png`);
65 NodeFS.writeFileSync(pngPath, Buffer.from("hello"));
66​
67 const resolved = resolveAttachmentPathById({
68 attachmentsDir,
69 attachmentId,
70 });
71 expect(resolved).toBe(pngPath);
72 } finally {
73 NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
74 }
75 });
76​
77 it("returns null when no attachment file exists for the id", () => {
78 const attachmentsDir = NodeFS.mkdtempSync(
79 NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"),
80 );
81 try {
82 const resolved = resolveAttachmentPathById({
83 attachmentsDir,
84 attachmentId: "thread-1-missing",
85 });
86 expect(resolved).toBeNull();
87 } finally {
88 NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
89 }
90 });
91});