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

assets.test.ts

95 lines2.8 KB

1import * as Schema from "effect/Schema";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 AssetAccessError,
6 AssetWorkspaceAssetNotFoundError,
7 AttachmentCreateUploadUrlInput,
8} from "./assets.ts";
9import { ThreadId } from "./baseSchemas.ts";
10import {
11 PROVIDER_SEND_TURN_MAX_FILE_BYTES,
12 PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
13} from "./chatAttachment.ts";
14​
15const isUploadInput = Schema.is(AttachmentCreateUploadUrlInput);
16const encodeAccessError = Schema.encodeUnknownSync(AssetAccessError);
17const decodeAccessError = Schema.decodeUnknownSync(AssetAccessError);
18​
19const uploadInput = {
20 name: "screenshot.png",
21 mimeType: "image/png",
22 sizeBytes: 3,
23} as const;
24​
25describe("AttachmentCreateUploadUrlInput", () => {
26 it("accepts supported image attachments", () => {
27 expect(isUploadInput(uploadInput)).toBe(true);
28 });
29​
30 it("rejects image types that providers do not support", () => {
31 expect(isUploadInput({ ...uploadInput, mimeType: "image/svg+xml" })).toBe(false);
32 });
33​
34 it("accepts generic files without treating them as provider images", () => {
35 expect(
36 isUploadInput({
37 type: "file",
38 name: "report.pdf",
39 mimeType: "application/pdf",
40 sizeBytes: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1,
41 }),
42 ).toBe(true);
43 expect(
44 isUploadInput({
45 type: "file",
46 name: "diagram.svg",
47 mimeType: "image/svg+xml",
48 sizeBytes: 3,
49 }),
50 ).toBe(true);
51 });
52​
53 it("rejects empty and oversized uploads", () => {
54 expect(isUploadInput({ ...uploadInput, sizeBytes: 0 })).toBe(false);
55 expect(
56 isUploadInput({ ...uploadInput, sizeBytes: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1 }),
57 ).toBe(false);
58 expect(
59 isUploadInput({
60 type: "file",
61 name: "archive.zip",
62 mimeType: "application/zip",
63 sizeBytes: PROVIDER_SEND_TURN_MAX_FILE_BYTES + 1,
64 }),
65 ).toBe(false);
66 });
67});
68​
69describe("AssetWorkspaceAssetNotFoundError", () => {
70 const resource = {
71 _tag: "media-file" as const,
72 threadId: ThreadId.make("thread-1"),
73 path: "~/Downloads/clip.mp4",
74 };
75​
76 it("carries the checked path across the wire", () => {
77 const encoded = encodeAccessError(
78 new AssetWorkspaceAssetNotFoundError({
79 resource,
80 checkedPath: "/Users/graham/Downloads/clip.mp4",
81 }),
82 );
83 const error = decodeAccessError(JSON.parse(JSON.stringify(encoded)));
84​
85 expect(error).toBeInstanceOf(AssetWorkspaceAssetNotFoundError);
86 expect(error).toMatchObject({ checkedPath: "/Users/graham/Downloads/clip.mp4" });
87 });
88​
89 it("decodes an error from a server that sends no checked path", () => {
90 const error = decodeAccessError({ _tag: "AssetWorkspaceAssetNotFoundError", resource });
91​
92 expect(error).toBeInstanceOf(AssetWorkspaceAssetNotFoundError);
93 expect(error).not.toHaveProperty("checkedPath");
94 });
95});