The best way to control Claude Code, Codex and more from any computer or phone

main
ts 155 lines 4.56 KB
Raw
1 import { describe, expect, it } from "@effect/vitest";
2 import {
3 EnvironmentId,
4 type AttachmentCreateUploadUrlInput,
5 type AttachmentCreateUploadUrlResult,
6 type AttachmentDeleteInput,
7 } from "@t3tools/contracts";
8 import * as Effect from "effect/Effect";
9 import * as Fiber from "effect/Fiber";
10 import { Atom, AtomRegistry } from "effect/reactivity";
11
12 import type { AtomCommand } from "./runtime.ts";
13 import {
14 formatAttachmentSize,
15 runAttachmentUploadCycle,
16 verifyPersistedAttachmentUpload,
17 } from "./attachments.ts";
18
19 const environmentId = EnvironmentId.make("environment-1");
20 const registry = {} as AtomRegistry.AtomRegistry;
21
22 type CreateUploadUrlCommand = AtomCommand<
23 { readonly environmentId: EnvironmentId; readonly input: AttachmentCreateUploadUrlInput },
24 AttachmentCreateUploadUrlResult,
25 never
26 >;
27
28 type RemoveCommand = AtomCommand<
29 { readonly environmentId: EnvironmentId; readonly input: AttachmentDeleteInput },
30 unknown,
31 never
32 >;
33
34 function makeCreateUploadUrl(attachmentId: string): CreateUploadUrlCommand {
35 return {
36 label: "test:create-upload-url",
37 run: () =>
38 Effect.succeed({
39 attachmentId,
40 relativeUrl: `/api/attachments/upload/${attachmentId}`,
41 expiresAt: 1,
42 }),
43 };
44 }
45
46 const removeCalls: string[] = [];
47 const remove: RemoveCommand = {
48 label: "test:remove",
49 run: (_registry, input) =>
50 Effect.sync(() => {
51 removeCalls.push(input.input.attachmentId);
52 }),
53 };
54
55 const uploadInput: AttachmentCreateUploadUrlInput = {
56 type: "file",
57 name: "report.pdf",
58 mimeType: "application/pdf",
59 sizeBytes: 3,
60 };
61
62 describe("runAttachmentUploadCycle", () => {
63 it.effect("aborts the transfer and deletes the fresh mint when interrupted", () =>
64 Effect.gen(function* () {
65 removeCalls.length = 0;
66 const transferStarted = Promise.withResolvers<void>();
67 let aborted = false;
68 const cycle = yield* Effect.forkChild(
69 runAttachmentUploadCycle({
70 registry,
71 createUploadUrl: makeCreateUploadUrl("pending-cancelled"),
72 remove,
73 environmentId,
74 upload: uploadInput,
75 resolveUploadUrl: () => "https://environment.test/upload",
76 transport: () =>
77 Effect.callback<void>(() => {
78 transferStarted.resolve();
79 return Effect.sync(() => {
80 aborted = true;
81 });
82 }),
83 }),
84 );
85 yield* Effect.promise(() => transferStarted.promise);
86 yield* Fiber.interrupt(cycle);
87
88 expect(aborted).toBe(true);
89 expect(removeCalls).toEqual(["pending-cancelled"]);
90 }),
91 );
92
93 it.effect("keeps the minted id on transfer failure so the caller can retry or release", () =>
94 Effect.gen(function* () {
95 removeCalls.length = 0;
96 const result = yield* runAttachmentUploadCycle({
97 registry,
98 createUploadUrl: makeCreateUploadUrl("pending-failed"),
99 remove,
100 environmentId,
101 upload: uploadInput,
102 resolveUploadUrl: () => "https://environment.test/upload",
103 transport: () => Effect.fail({ _tag: "UploadRejected", status: 413 } as const),
104 });
105
106 expect(result).toMatchObject({
107 status: "failed",
108 step: "transfer",
109 attachmentId: "pending-failed",
110 });
111 expect(removeCalls).toEqual([]);
112 }),
113 );
114 });
115
116 describe("verifyPersistedAttachmentUpload", () => {
117 it.effect("hits the server on every verification instead of reusing a cached failure", () =>
118 Effect.gen(function* () {
119 let lookups = 0;
120 const assetUrlAtom = Atom.make(
121 Effect.promise(() => Promise.resolve()).pipe(
122 Effect.flatMap(() => {
123 lookups += 1;
124 return lookups === 1
125 ? Effect.fail({ _tag: "TransportError" } as const)
126 : Effect.succeed({ url: "/api/assets/pending-1" });
127 }),
128 ),
129 ).pipe(Atom.swr({ staleTime: 60_000 }), Atom.keepAlive);
130 const liveRegistry = AtomRegistry.make();
131
132 const verify = verifyPersistedAttachmentUpload({
133 registry: liveRegistry,
134 createAssetUrl: () => assetUrlAtom,
135 environmentId,
136 attachmentId: "pending-1",
137 });
138
139 expect(yield* verify).toMatchObject({ status: "failed" });
140 expect(yield* verify).toEqual({ status: "verified" });
141 expect(lookups).toBe(2);
142 }),
143 );
144 });
145
146 describe("formatAttachmentSize", () => {
147 it.each([
148 [1_047_552, "1023 KB"],
149 [1_047_553, "1.0 MB"],
150 [1_048_575, "1.0 MB"],
151 [1_048_576, "1.0 MB"],
152 ])("formats %d bytes as %s", (sizeBytes, label) => {
153 expect(formatAttachmentSize(sizeBytes)).toBe(label);
154 });
155 });