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

attachmentStore.test.ts

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