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

attachments.test.ts

107 lines3.9 KB

1import {
2 AuthOrchestrationOperateScope,
3 EnvironmentAuthorizationError,
4 EnvironmentId,
5} from "@t3tools/contracts";
6import { runAttachmentUploadCycle } from "@t3tools/client-runtime/state/attachments";
7import * as Cause from "effect/Cause";
8import { AsyncResult, AtomRegistry } from "effect/reactivity";
9import { describe, expect, it } from "@effect/vitest";
10import * as Effect from "effect/Effect";
11import * as Fiber from "effect/Fiber";
12import { afterEach, beforeEach, vi } from "vite-plus/test";
13​
14const state = vi.hoisted(() => ({
15 grantedEnvironments: new Set<string>(),
16 create: vi.fn(),
17 remove: vi.fn(),
18}));
19vi.mock("../connection/runtime", () => ({ connectionAtomRuntime: {} }));
20vi.mock("./session", () => ({
21 readEnvironmentScope: (environmentId: string, scope: string) =>
22 scope === AuthOrchestrationOperateScope && state.grantedEnvironments.has(environmentId),
23}));
24vi.mock("@t3tools/client-runtime/state/attachments", async (importOriginal) => ({
25 ...(await importOriginal<typeof import("@t3tools/client-runtime/state/attachments")>()),
26 createAttachmentEnvironmentAtoms: () => ({
27 createUploadUrl: { label: "create", run: state.create },
28 remove: { label: "remove", run: state.remove },
29 }),
30}));
31​
32import { attachmentEnvironment } from "./attachments";
33​
34const environmentId = EnvironmentId.make("secondary");
35const registry = AtomRegistry.make();
36beforeEach(() => {
37 state.grantedEnvironments = new Set(["primary"]);
38 state.create.mockReset().mockResolvedValue(
39 AsyncResult.success({
40 attachmentId: "pending",
41 relativeUrl: "/upload",
42 expiresAt: 1,
43 }),
44 );
45 state.remove.mockReset().mockResolvedValue(AsyncResult.success(undefined));
46});
47afterEach(() => {
48 vi.restoreAllMocks();
49});
50​
51describe("attachment mutation grants", () => {
52 it("uses the attachment environment for both mint and deletion", async () => {
53 const mintResult = await attachmentEnvironment.createUploadUrl.run(registry, {
54 environmentId,
55 input: { name: "image.png", mimeType: "image/png", sizeBytes: 1 },
56 });
57 const removeResult = await attachmentEnvironment.remove.run(registry, {
58 environmentId,
59 input: { attachmentId: "pending" },
60 });
61 for (const result of [mintResult, removeResult]) {
62 expect(result._tag).toBe("Failure");
63 if (result._tag !== "Failure") throw new Error("Expected permission denial");
64 const error = Cause.squash<unknown>(result.cause);
65 expect(error).toBeInstanceOf(EnvironmentAuthorizationError);
66 expect(error).toMatchObject({ requiredScope: AuthOrchestrationOperateScope });
67 }
68 expect(state.create).not.toHaveBeenCalled();
69 expect(state.remove).not.toHaveBeenCalled();
70 state.grantedEnvironments.add("secondary");
71 expect(
72 (
73 await attachmentEnvironment.remove.run(registry, {
74 environmentId,
75 input: { attachmentId: "pending" },
76 })
77 )._tag,
78 ).toBe("Success");
79 expect(state.remove).toHaveBeenCalledOnce();
80 });
81​
82 it.effect("blocks the upload cycle's cleanup after grant revocation", () =>
83 Effect.gen(function* () {
84 state.grantedEnvironments.add("secondary");
85 const transferStarted = Promise.withResolvers<void>();
86 const cycle = yield* Effect.forkChild(
87 runAttachmentUploadCycle({
88 registry,
89 ...attachmentEnvironment,
90 environmentId,
91 upload: { name: "image.png", mimeType: "image/png", sizeBytes: 1 },
92 resolveUploadUrl: () => "https://example.test/upload",
93 transport: () =>
94 Effect.callback<void>(() => {
95 state.grantedEnvironments.delete("secondary");
96 transferStarted.resolve();
97 }),
98 }),
99 );
100 yield* Effect.promise(() => transferStarted.promise);
101 // Interrupting deletes the fresh mint, which the revoked grant must refuse.
102 yield* Fiber.interrupt(cycle);
103 expect(state.create).toHaveBeenCalledOnce();
104 expect(state.remove).not.toHaveBeenCalled();
105 }),
106 );
107});