Real bugs from Macroscope and Bugbot, all verified against source: - awaitAttachmentUploads consulted only live jobs, so an upload that settled before the await lost its result and a ready image could drop from a pick-and-send message. Terminal states are now kept in a settled map that cancel/release clean up. - Stashing cancelled in-flight uploads before the localStorage write was confirmed; a quota failure stranded chips in uploading forever. Cancellation now happens only after the write lands. - Retargeting a draft to another environment overwrote a reload-restored attachment's ready state with a dead failed state. The mismatch is now derived (never written), so switching back recovers the attachment, and re-uploads release the old environment's bytes first. - The turn-start failure path restored chips from the pre-await snapshot, resurrecting uploading states no job would ever advance. - The "images were not attached" toast could fire when no message was sent; it now waits for the turn to actually start. - Removing a preview annotation or discarding a draft leaked the upload and its server-side bytes; both now release like chip removal. - Mobile paste drops now surface the needs-an-update banner instead of a console.warn. - Normalizer error mappings keep their PlatformError causes; the dead unstructured AttachmentUploadRequestError is deleted; the sweep test no longer reads the real clock (effect-diagnostics CI failure). Also folds the rebase onto main: the new moveComposerPromptAndImages draft-carry now merges the unified image list, and in-flight uploads are retargeted to the destination draft so moved chips keep their progress. Co-Authored-By: Claude Fable 5 <[email protected]>
237 lines
8.3 KiB
TypeScript
237 lines
8.3 KiB
TypeScript
// @effect-diagnostics nodeBuiltinImport:off
|
|
import * as NodeFS from "node:fs";
|
|
import * as NodeOS from "node:os";
|
|
import * as NodePath from "node:path";
|
|
|
|
import { describe, expect, it } from "vite-plus/test";
|
|
|
|
import {
|
|
createAttachmentId,
|
|
createPendingAttachmentId,
|
|
parseThreadSegmentFromAttachmentId,
|
|
planAttachmentClaim,
|
|
resolveAttachmentPathById,
|
|
sweepStalePendingAttachments,
|
|
toSafeThreadAttachmentSegment,
|
|
PENDING_ATTACHMENT_MAX_AGE_MS,
|
|
} from "./attachmentStore.ts";
|
|
|
|
function makeTempAttachmentsDir(): string {
|
|
return NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"));
|
|
}
|
|
|
|
describe("attachmentStore", () => {
|
|
it("sanitizes thread ids when creating attachment ids", () => {
|
|
const attachmentId = createAttachmentId("thread.folder/unsafe space");
|
|
expect(attachmentId).toBeTruthy();
|
|
if (!attachmentId) {
|
|
return;
|
|
}
|
|
|
|
const threadSegment = parseThreadSegmentFromAttachmentId(attachmentId);
|
|
expect(threadSegment).toBeTruthy();
|
|
expect(threadSegment).toMatch(/^[a-z0-9_-]+$/i);
|
|
expect(threadSegment).not.toContain(".");
|
|
expect(threadSegment).not.toContain("%");
|
|
expect(threadSegment).not.toContain("/");
|
|
});
|
|
|
|
it("parses exact thread segments from attachment ids without prefix collisions", () => {
|
|
const fooId = "foo-00000000-0000-4000-8000-000000000001";
|
|
const fooBarId = "foo-bar-00000000-0000-4000-8000-000000000002";
|
|
|
|
expect(parseThreadSegmentFromAttachmentId(fooId)).toBe("foo");
|
|
expect(parseThreadSegmentFromAttachmentId(fooBarId)).toBe("foo-bar");
|
|
});
|
|
|
|
it("normalizes created thread segments to lowercase", () => {
|
|
const attachmentId = createAttachmentId("Thread.Foo");
|
|
expect(attachmentId).toBeTruthy();
|
|
if (!attachmentId) {
|
|
return;
|
|
}
|
|
expect(parseThreadSegmentFromAttachmentId(attachmentId)).toBe("thread-foo");
|
|
});
|
|
|
|
it("resolves attachment path by id using the extension that exists on disk", () => {
|
|
const attachmentsDir = makeTempAttachmentsDir();
|
|
try {
|
|
const attachmentId = "thread-1-00000000-0000-4000-8000-00000000000a";
|
|
const pngPath = NodePath.join(attachmentsDir, `${attachmentId}.png`);
|
|
NodeFS.writeFileSync(pngPath, Buffer.from("hello"));
|
|
|
|
const resolved = resolveAttachmentPathById({
|
|
attachmentsDir,
|
|
attachmentId,
|
|
});
|
|
expect(resolved).toBe(pngPath);
|
|
} finally {
|
|
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("resolves a pending id to the renamed thread-scoped file after claim", () => {
|
|
const attachmentsDir = makeTempAttachmentsDir();
|
|
try {
|
|
const uuid = "00000000-0000-4000-8000-00000000000b";
|
|
const scopedPath = NodePath.join(attachmentsDir, `thread-9-${uuid}.png`);
|
|
NodeFS.writeFileSync(scopedPath, Buffer.from("pixels"));
|
|
|
|
// Old signed asset URLs carry the pending id; they must keep resolving.
|
|
const resolved = resolveAttachmentPathById({
|
|
attachmentsDir,
|
|
attachmentId: `pending-${uuid}`,
|
|
});
|
|
expect(resolved).toBe(scopedPath);
|
|
} finally {
|
|
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("reserves the pending segment for uploads", () => {
|
|
expect(toSafeThreadAttachmentSegment("pending")).toBe("pending_thread");
|
|
const pendingId = createPendingAttachmentId();
|
|
expect(parseThreadSegmentFromAttachmentId(pendingId)).toBe("pending");
|
|
});
|
|
|
|
describe("planAttachmentClaim", () => {
|
|
const uuid = "00000000-0000-4000-8000-00000000000c";
|
|
|
|
it("plans a rename for a pending attachment", () => {
|
|
const attachmentsDir = makeTempAttachmentsDir();
|
|
try {
|
|
const pendingPath = NodePath.join(attachmentsDir, `pending-${uuid}.webp`);
|
|
NodeFS.writeFileSync(pendingPath, Buffer.from("pixels"));
|
|
|
|
const claimPlan = planAttachmentClaim({
|
|
attachmentsDir,
|
|
threadId: "Thread.Foo",
|
|
attachmentId: `pending-${uuid}`,
|
|
});
|
|
expect(claimPlan).toEqual({
|
|
ok: true,
|
|
finalId: `thread-foo-${uuid}`,
|
|
currentPath: pendingPath,
|
|
finalPath: NodePath.join(attachmentsDir, `thread-foo-${uuid}.webp`),
|
|
alreadyScoped: false,
|
|
});
|
|
} finally {
|
|
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("is idempotent when a retry references an already-renamed file", () => {
|
|
const attachmentsDir = makeTempAttachmentsDir();
|
|
try {
|
|
const scopedPath = NodePath.join(attachmentsDir, `thread-foo-${uuid}.webp`);
|
|
NodeFS.writeFileSync(scopedPath, Buffer.from("pixels"));
|
|
|
|
const claimPlan = planAttachmentClaim({
|
|
attachmentsDir,
|
|
threadId: "Thread.Foo",
|
|
attachmentId: `pending-${uuid}`,
|
|
});
|
|
expect(claimPlan).toMatchObject({ ok: true, alreadyScoped: true });
|
|
} finally {
|
|
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("refuses an attachment claimed by another thread", () => {
|
|
const attachmentsDir = makeTempAttachmentsDir();
|
|
try {
|
|
NodeFS.writeFileSync(
|
|
NodePath.join(attachmentsDir, `other-thread-${uuid}.png`),
|
|
Buffer.from("pixels"),
|
|
);
|
|
|
|
const claimPlan = planAttachmentClaim({
|
|
attachmentsDir,
|
|
threadId: "Thread.Foo",
|
|
attachmentId: `pending-${uuid}`,
|
|
});
|
|
expect(claimPlan).toMatchObject({ ok: false });
|
|
} finally {
|
|
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("reports a missing attachment", () => {
|
|
const attachmentsDir = makeTempAttachmentsDir();
|
|
try {
|
|
const claimPlan = planAttachmentClaim({
|
|
attachmentsDir,
|
|
threadId: "Thread.Foo",
|
|
attachmentId: `pending-${uuid}`,
|
|
});
|
|
expect(claimPlan).toMatchObject({ ok: false });
|
|
} finally {
|
|
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("sweepStalePendingAttachments", () => {
|
|
it("deletes stale pending and partial files, keeps fresh and scoped ones", () => {
|
|
const attachmentsDir = makeTempAttachmentsDir();
|
|
try {
|
|
const stalePending = NodePath.join(
|
|
attachmentsDir,
|
|
"pending-00000000-0000-4000-8000-000000000001.png",
|
|
);
|
|
const freshPending = NodePath.join(
|
|
attachmentsDir,
|
|
"pending-00000000-0000-4000-8000-000000000002.png",
|
|
);
|
|
const scoped = NodePath.join(
|
|
attachmentsDir,
|
|
"thread-1-00000000-0000-4000-8000-000000000003.png",
|
|
);
|
|
const stalePart = NodePath.join(
|
|
attachmentsDir,
|
|
"pending-00000000-0000-4000-8000-000000000004.png.part",
|
|
);
|
|
// Fixed epoch so the test never touches the real clock: files are
|
|
// stamped relative to `nowMs` below, not to wall time.
|
|
const nowMs = 1_800_000_000_000;
|
|
const freshMs = nowMs - 60_000;
|
|
const staleMs = nowMs - PENDING_ATTACHMENT_MAX_AGE_MS - 60_000;
|
|
for (const filePath of [stalePending, freshPending, scoped, stalePart]) {
|
|
NodeFS.writeFileSync(filePath, Buffer.from("x"));
|
|
}
|
|
NodeFS.utimesSync(freshPending, freshMs / 1000, freshMs / 1000);
|
|
NodeFS.utimesSync(stalePending, staleMs / 1000, staleMs / 1000);
|
|
NodeFS.utimesSync(scoped, staleMs / 1000, staleMs / 1000);
|
|
NodeFS.utimesSync(stalePart, staleMs / 1000, staleMs / 1000);
|
|
|
|
const swept = sweepStalePendingAttachments({
|
|
attachmentsDir,
|
|
nowMs,
|
|
});
|
|
expect(swept.deleted).toBe(2);
|
|
expect(NodeFS.existsSync(stalePending)).toBe(false);
|
|
expect(NodeFS.existsSync(stalePart)).toBe(false);
|
|
expect(NodeFS.existsSync(freshPending)).toBe(true);
|
|
expect(NodeFS.existsSync(scoped)).toBe(true);
|
|
} finally {
|
|
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
it("returns null when no attachment file exists for the id", () => {
|
|
const attachmentsDir = NodeFS.mkdtempSync(
|
|
NodePath.join(NodeOS.tmpdir(), "t3code-attachment-store-"),
|
|
);
|
|
try {
|
|
const resolved = resolveAttachmentPathById({
|
|
attachmentsDir,
|
|
attachmentId: "thread-1-missing",
|
|
});
|
|
expect(resolved).toBeNull();
|
|
} finally {
|
|
NodeFS.rmSync(attachmentsDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|