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

attachmentStore.test.ts

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