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

attachmentStore.test.ts

102 lines3.6 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("keeps deterministic ids distinct when sanitized thread segments collide", () => {
27 const dotted = createDeterministicAttachmentId("thread.a", "message-1:0");
28 const dashed = createDeterministicAttachmentId("thread-a", "message-1:0");
29​
30 expect(dotted).toBeTruthy();
31 expect(dashed).toBeTruthy();
32 expect(dotted).not.toBe(dashed);
33 expect(dotted && parseThreadSegmentFromAttachmentId(dotted)).toBe("thread-a");
34 expect(dashed && parseThreadSegmentFromAttachmentId(dashed)).toBe("thread-a");
35 });
36​
37 it("sanitizes thread ids when creating attachment ids", () => {
38 const attachmentId = createAttachmentId("thread.folder/unsafe space");
39 expect(attachmentId).toBeTruthy();
40 if (!attachmentId) {
41 return;
42 }
43​
44 const threadSegment = parseThreadSegmentFromAttachmentId(attachmentId);
45 expect(threadSegment).toBeTruthy();
46 expect(threadSegment).toMatch(/^[a-z0-9_-]+$/i);
47 expect(threadSegment).not.toContain(".");
48 expect(threadSegment).not.toContain("%");
49 expect(threadSegment).not.toContain("/");
50 });
51​
52 it("parses exact thread segments from attachment ids without prefix collisions", () => {
53 const fooId = "foo-00000000-0000-4000-8000-000000000001";
54 const fooBarId = "foo-bar-00000000-0000-4000-8000-000000000002";
55​
56 expect(parseThreadSegmentFromAttachmentId(fooId)).toBe("foo");
57 expect(parseThreadSegmentFromAttachmentId(fooBarId)).toBe("foo-bar");
58 });
59​
60 it("normalizes created thread segments to lowercase", () => {
61 const attachmentId = createAttachmentId("Thread.Foo");
62 expect(attachmentId).toBeTruthy();
63 if (!attachmentId) {
64 return;
65 }
66 expect(parseThreadSegmentFromAttachmentId(attachmentId)).toBe("thread-foo");
67 });
68​
69 it("resolves attachment path by id using the extension that exists on disk", () => {
70 const attachmentsDir = NodeFS.mkdtempSync(
71 NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"),
72 );
73 try {
74 const attachmentId = "thread-1-attachment";
75 const pngPath = NodePath.join(attachmentsDir, `${attachmentId}.png`);
76 NodeFS.writeFileSync(pngPath, Buffer.from("hello"));
77​
78 const resolved = resolveAttachmentPathById({
79 attachmentsDir,
80 attachmentId,
81 });
82 expect(resolved).toBe(pngPath);
83 } finally {
84 NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
85 }
86 });
87​
88 it("returns null when no attachment file exists for the id", () => {
89 const attachmentsDir = NodeFS.mkdtempSync(
90 NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"),
91 );
92 try {
93 const resolved = resolveAttachmentPathById({
94 attachmentsDir,
95 attachmentId: "thread-1-missing",
96 });
97 expect(resolved).toBeNull();
98 } finally {
99 NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
100 }
101 });
102});