Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import {
3 EnvironmentId,
4 type AttachmentCreateUploadUrlInput,
5 type AttachmentCreateUploadUrlResult,
6 type AttachmentDeleteInput,
7} from "@t3tools/contracts";
8import * as Effect from "effect/Effect";
9import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
10
11import type { AtomCommand } from "./runtime.ts";
12import { runAttachmentUploadCycle, verifyPersistedAttachmentUpload } from "./attachments.ts";
13
14const environmentId = EnvironmentId.make("environment-1");
15const registry = {} as AtomRegistry.AtomRegistry;
16
17type CreateUploadUrlCommand = AtomCommand<
18 { readonly environmentId: EnvironmentId; readonly input: AttachmentCreateUploadUrlInput },
19 AttachmentCreateUploadUrlResult,
20 never
21>;
22
23type RemoveCommand = AtomCommand<
24 { readonly environmentId: EnvironmentId; readonly input: AttachmentDeleteInput },
25 unknown,
26 never
27>;
28
29function makeCreateUploadUrl(attachmentId: string): CreateUploadUrlCommand {
30 return {
31 label: "test:create-upload-url",
32 run: async () =>
33 AsyncResult.success<AttachmentCreateUploadUrlResult, never>({
34 attachmentId,
35 relativeUrl: `/api/attachments/upload/${attachmentId}`,
36 expiresAt: 1,
37 }),
38 };
39}
40
41const removeCalls: string[] = [];
42const remove: RemoveCommand = {
43 label: "test:remove",
44 run: async (_registry, input) => {
45 removeCalls.push(input.input.attachmentId);
46 return AsyncResult.success(undefined);
47 },
48};
49
50const uploadInput: AttachmentCreateUploadUrlInput = {
51 type: "file",
52 name: "report.pdf",
53 mimeType: "application/pdf",
54 sizeBytes: 3,
55};
56
57describe("runAttachmentUploadCycle", () => {
58 it("deletes the fresh mint when the caller cancels at onMinted", async () => {
59 removeCalls.length = 0;
60 const result = await runAttachmentUploadCycle({
61 registry,
62 createUploadUrl: makeCreateUploadUrl("pending-cancelled"),
63 remove,
64 environmentId,
65 upload: uploadInput,
66 resolveUploadUrl: () => "https://environment.test/upload",
67 transport: () => {
68 throw new Error("transport must not run after cancel");
69 },
70 onMinted: () => "cancel",
71 });
72
73 expect(result).toEqual({ status: "cancelled", attachmentId: "pending-cancelled" });
74 expect(removeCalls).toEqual(["pending-cancelled"]);
75 });
76
77 it("keeps the minted id on transfer failure so the caller can retry or release", async () => {
78 removeCalls.length = 0;
79 const result = await runAttachmentUploadCycle({
80 registry,
81 createUploadUrl: makeCreateUploadUrl("pending-failed"),
82 remove,
83 environmentId,
84 upload: uploadInput,
85 resolveUploadUrl: () => "https://environment.test/upload",
86 transport: () => ({
87 done: Promise.reject(new Error("Upload rejected (413)")),
88 abort: () => {},
89 }),
90 });
91
92 expect(result).toMatchObject({
93 status: "failed",
94 step: "transfer",
95 attachmentId: "pending-failed",
96 });
97 expect(removeCalls).toEqual([]);
98 });
99});
100
101describe("verifyPersistedAttachmentUpload", () => {
102 it("hits the server on every verification instead of reusing a cached failure", async () => {
103 let lookups = 0;
104 const assetUrlAtom = Atom.make(
105 Effect.promise(() => Promise.resolve()).pipe(
106 Effect.flatMap(() => {
107 lookups += 1;
108 return lookups === 1
109 ? Effect.fail({ _tag: "TransportError" } as const)
110 : Effect.succeed({ url: "/api/assets/pending-1" });
111 }),
112 ),
113 ).pipe(Atom.swr({ staleTime: 60_000 }), Atom.keepAlive);
114 const liveRegistry = AtomRegistry.make();
115
116 const verify = () =>
117 verifyPersistedAttachmentUpload({
118 registry: liveRegistry,
119 createAssetUrl: () => assetUrlAtom,
120 environmentId,
121 attachmentId: "pending-1",
122 });
123
124 const first = await verify();
125 expect(first).toMatchObject({ status: "failed" });
126
127 const second = await verify();
128 expect(second).toEqual({ status: "verified" });
129 expect(lookups).toBe(2);
130 });
131});