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

attachmentStore.test.ts

221 lines8.7 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 attachmentFileExtension,
10 createAttachmentId,
11 createDeterministicAttachmentId,
12 createPendingAttachmentId,
13 parseAttachmentUuid,
14 parseAttachmentFileExtension,
15 planAttachmentClaim,
16 parseThreadSegmentFromAttachmentId,
17 resolveAttachmentPathById,
18 sweepStalePendingAttachments,
19} from "./attachmentStore.ts";
20​
21describe("attachmentStore", () => {
22 it("derives stable attachment ids for idempotent message retries", () => {
23 const first = createDeterministicAttachmentId("thread-1", "message-1:0");
24 const retry = createDeterministicAttachmentId("thread-1", "message-1:0");
25 const next = createDeterministicAttachmentId("thread-1", "message-1:1");
26​
27 expect(first).toBe(retry);
28 expect(next).not.toBe(first);
29 expect(first && parseThreadSegmentFromAttachmentId(first)).toBe("thread-1");
30 });
31​
32 it("keeps deterministic ids distinct when sanitized thread segments collide", () => {
33 const dotted = createDeterministicAttachmentId("thread.a", "message-1:0");
34 const dashed = createDeterministicAttachmentId("thread-a", "message-1:0");
35​
36 expect(dotted).toBeTruthy();
37 expect(dashed).toBeTruthy();
38 expect(dotted).not.toBe(dashed);
39 expect(dotted && parseThreadSegmentFromAttachmentId(dotted)).toBe("thread-a");
40 expect(dashed && parseThreadSegmentFromAttachmentId(dashed)).toBe("thread-a");
41 });
42​
43 it("sanitizes thread ids when creating attachment ids", () => {
44 const attachmentId = createAttachmentId("thread.folder/unsafe space");
45 expect(attachmentId).toBeTruthy();
46 if (!attachmentId) {
47 return;
48 }
49​
50 const threadSegment = parseThreadSegmentFromAttachmentId(attachmentId);
51 expect(threadSegment).toBeTruthy();
52 expect(threadSegment).toMatch(/^[a-z0-9_-]+$/i);
53 expect(threadSegment).not.toContain(".");
54 expect(threadSegment).not.toContain("%");
55 expect(threadSegment).not.toContain("/");
56 });
57​
58 it("parses exact thread segments from attachment ids without prefix collisions", () => {
59 const fooId = "foo-00000000-0000-4000-8000-000000000001";
60 const fooBarId = "foo-bar-00000000-0000-4000-8000-000000000002";
61​
62 expect(parseThreadSegmentFromAttachmentId(fooId)).toBe("foo");
63 expect(parseThreadSegmentFromAttachmentId(fooBarId)).toBe("foo-bar");
64 });
65​
66 it("normalizes created thread segments to lowercase", () => {
67 const attachmentId = createAttachmentId("Thread.Foo");
68 expect(attachmentId).toBeTruthy();
69 if (!attachmentId) {
70 return;
71 }
72 expect(parseThreadSegmentFromAttachmentId(attachmentId)).toBe("thread-foo");
73 });
74​
75 it("reserves the pending attachment segment", () => {
76 const pendingId = createPendingAttachmentId();
77 expect(parseThreadSegmentFromAttachmentId(pendingId)).toBe("pending");
78 expect(parseAttachmentUuid(pendingId)).toMatch(/^[a-f0-9-]{36}$/);
79 expect(parseThreadSegmentFromAttachmentId(createAttachmentId("pending")!)).toBe("_pending");
80 expect(parseThreadSegmentFromAttachmentId(createAttachmentId("pending_thread")!)).toBe(
81 "pending_thread",
82 );
83 });
84​
85 it("preserves safe file extensions in attachment ids and paths", () => {
86 const attachmentId = createPendingAttachmentId(".PDF");
87​
88 expect(parseThreadSegmentFromAttachmentId(attachmentId)).toBe("pending");
89 expect(parseAttachmentUuid(attachmentId)).toMatch(/^[a-f0-9-]{36}$/);
90 expect(parseAttachmentFileExtension(attachmentId)).toBe("pdf");
91 expect(attachmentFileExtension("report.PDF")).toBe(".pdf");
92 expect(attachmentFileExtension("report")).toBe(".bin");
93 expect(attachmentFileExtension("report.extensiontoolong")).toBe(".bin");
94 // ".part" is the in-flight upload suffix; storing it would make the file
95 // look like a stale partial to the sweep.
96 expect(attachmentFileExtension("archive.part")).toBe(".bin");
97 expect(createAttachmentId("x".repeat(80), ".abcdefghij")?.length).toBeLessThanOrEqual(128);
98 });
99​
100 it("resolves attachment path by id using the extension that exists on disk", () => {
101 const attachmentsDir = NodeFS.mkdtempSync(
102 NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"),
103 );
104 try {
105 const attachmentId = "thread-1-attachment";
106 const pngPath = NodePath.join(attachmentsDir, `${attachmentId}.png`);
107 NodeFS.writeFileSync(pngPath, Buffer.from("hello"));
108​
109 const resolved = resolveAttachmentPathById({
110 attachmentsDir,
111 attachmentId,
112 });
113 expect(resolved).toBe(pngPath);
114 } finally {
115 NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
116 }
117 });
118​
119 it("returns null when no attachment file exists for the id", () => {
120 const attachmentsDir = NodeFS.mkdtempSync(
121 NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"),
122 );
123 try {
124 const resolved = resolveAttachmentPathById({
125 attachmentsDir,
126 attachmentId: "thread-1-missing",
127 });
128 expect(resolved).toBeNull();
129 } finally {
130 NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
131 }
132 });
133​
134 it("resolves generic attachments without scanning the attachment directory", () => {
135 const attachmentsDir = NodeFS.mkdtempSync(
136 NodePath.join(NodeOS.tmpdir(), "t3code-file-attachment-"),
137 );
138 try {
139 const attachmentId = "thread-1-00000000-0000-4000-8000-000000000001-zip";
140 const archivePath = NodePath.join(attachmentsDir, `${attachmentId}.zip`);
141 NodeFS.writeFileSync(archivePath, Buffer.from("archive"));
142​
143 expect(resolveAttachmentPathById({ attachmentsDir, attachmentId })).toBe(archivePath);
144 } finally {
145 NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
146 }
147 });
148​
149 it("plans pending attachment claims with direct filename lookups", () => {
150 const attachmentsDir = NodeFS.mkdtempSync(
151 NodePath.join(NodeOS.tmpdir(), "t3code-attachment-claim-"),
152 );
153 try {
154 const uuid = "00000000-0000-4000-8000-000000000001";
155 const pendingPath = NodePath.join(attachmentsDir, `pending-${uuid}.png`);
156 NodeFS.writeFileSync(pendingPath, Buffer.from("pixels"));
157​
158 const claim = planAttachmentClaim({
159 attachmentsDir,
160 threadId: "thread-1",
161 attachmentId: `pending-${uuid}`,
162 });
163 expect(claim).toMatchObject({
164 ok: true,
165 currentPath: pendingPath,
166 });
167 if (!claim.ok) {
168 return;
169 }
170 expect(parseThreadSegmentFromAttachmentId(claim.finalId)).toBe("thread-1");
171 expect(parseAttachmentUuid(claim.finalId)).not.toBe(uuid);
172 expect(claim.finalPath).toBe(NodePath.join(attachmentsDir, `${claim.finalId}.png`));
173 } finally {
174 NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
175 }
176 });
177​
178 it("rejects thread-owned attachments even when thread segments collide", () => {
179 const attachmentsDir = NodeFS.mkdtempSync(
180 NodePath.join(NodeOS.tmpdir(), "t3code-attachment-ownership-"),
181 );
182 try {
183 const attachmentId = "a-b-00000000-0000-4000-8000-000000000003";
184 NodeFS.writeFileSync(NodePath.join(attachmentsDir, `${attachmentId}.png`), "pixels");
185​
186 expect(planAttachmentClaim({ attachmentsDir, threadId: "a b", attachmentId })).toEqual({
187 ok: false,
188 reason: "attachment must be a pending upload",
189 });
190 } finally {
191 NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
192 }
193 });
194​
195 it("removes expired pending and partial files without touching thread attachments", () => {
196 const attachmentsDir = NodeFS.mkdtempSync(
197 NodePath.join(NodeOS.tmpdir(), "t3code-attachment-sweep-"),
198 );
199 try {
200 const now = 1_800_000_000_000;
201 const oldTimeSeconds = (now - 2 * 24 * 60 * 60 * 1000) / 1000;
202 const uuid = "00000000-0000-4000-8000-000000000002";
203 const pendingPath = NodePath.join(attachmentsDir, `pending-${uuid}.png`);
204 const pendingFilePath = NodePath.join(attachmentsDir, `pending-${uuid}-pdf.pdf`);
205 const threadPath = NodePath.join(attachmentsDir, `thread-1-${uuid}.png`);
206 const partialPath = NodePath.join(attachmentsDir, `${uuid}.part`);
207 for (const filePath of [pendingPath, pendingFilePath, threadPath, partialPath]) {
208 NodeFS.writeFileSync(filePath, Buffer.from("pixels"));
209 NodeFS.utimesSync(filePath, oldTimeSeconds, oldTimeSeconds);
210 }
211​
212 expect(sweepStalePendingAttachments({ attachmentsDir, nowMs: now })).toEqual({ deleted: 3 });
213 expect(NodeFS.existsSync(pendingPath)).toBe(false);
214 expect(NodeFS.existsSync(pendingFilePath)).toBe(false);
215 expect(NodeFS.existsSync(partialPath)).toBe(false);
216 expect(NodeFS.existsSync(threadPath)).toBe(true);
217 } finally {
218 NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
219 }
220 });
221});