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

filesystem.test.ts

118 lines4.5 KB

1import * as Schema from "effect/Schema";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 FilesystemBrowseError,
6 FilesystemBrowseInput,
7 FilesystemBrowseResult,
8 FilesystemGetMetadataInput,
9} from "./filesystem.ts";
10​
11const encodeGetMetadataInput = Schema.encodeSync(FilesystemGetMetadataInput);
12const decodeGetMetadataInput = Schema.decodeUnknownSync(FilesystemGetMetadataInput);
13const encodeBrowseInput = Schema.encodeSync(FilesystemBrowseInput);
14const decodeBrowseInput = Schema.decodeUnknownSync(FilesystemBrowseInput);
15const encodeBrowseResult = Schema.encodeSync(FilesystemBrowseResult);
16const decodeBrowseResult = Schema.decodeUnknownSync(FilesystemBrowseResult);
17const encodeBrowseError = Schema.encodeSync(FilesystemBrowseError);
18const decodeBrowseError = Schema.decodeUnknownSync(FilesystemBrowseError);
19​
20describe("FilesystemGetMetadataInput", () => {
21 it("accepts nested paths up to 4096 characters and rejects longer paths", () => {
22 const deep = `/${"segment/".repeat(75)}deep.txt`;
23 const maxLen = `/${"a".repeat(4095)}`;
24 expect(decodeGetMetadataInput({ paths: [deep, maxLen] })).toEqual({ paths: [deep, maxLen] });
25 expect(() => decodeGetMetadataInput({ paths: [`/${"a".repeat(4096)}`] })).toThrow();
26 });
27​
28 it("preserves literal edge whitespace in paths while rejecting blank strings", () => {
29 const input = { paths: ["/tmp/notes ", " /tmp/leading"] };
30 const encoded = encodeGetMetadataInput(input);
31​
32 expect(encoded).toEqual(input);
33 expect(decodeGetMetadataInput(encoded)).toEqual(input);
34 expect(() => decodeGetMetadataInput({ paths: [""] })).toThrow();
35 expect(() => decodeGetMetadataInput({ paths: [" "] })).toThrow();
36 });
37});
38​
39describe("FilesystemBrowseInput", () => {
40 it("accepts paths up to 4096 characters and rejects longer ones", () => {
41 const maxLen = `/${"a".repeat(4095)}`;
42 const tooLong = `/${"a".repeat(4096)}`;
43 expect(decodeBrowseInput({ partialPath: maxLen, cwd: maxLen })).toEqual({
44 partialPath: maxLen,
45 cwd: maxLen,
46 });
47 expect(() => decodeBrowseInput({ partialPath: tooLong })).toThrow();
48 expect(() => decodeBrowseInput({ partialPath: "./child", cwd: tooLong })).toThrow();
49 });
50​
51 it("preserves literal edge whitespace in cwd while rejecting blank strings", () => {
52 const input = { partialPath: "./src", cwd: "/tmp/project " };
53 const encoded = encodeBrowseInput(input);
54​
55 expect(encoded).toEqual(input);
56 expect(decodeBrowseInput(encoded)).toEqual(input);
57 expect(() => decodeBrowseInput({ partialPath: "./src", cwd: " " })).toThrow();
58 });
59});
60​
61describe("FilesystemBrowseResult", () => {
62 it("round-trips the exact parent path when it ends in a space", () => {
63 const result = {
64 parentPath: "/tmp/t3-browse/repo ",
65 entries: [{ name: "child", fullPath: "/tmp/t3-browse/repo /child" }],
66 };
67 const encoded = encodeBrowseResult(result);
68​
69 expect(encoded).toEqual(result);
70 expect(decodeBrowseResult(encoded)).toEqual(result);
71 });
72});
73​
74describe("FilesystemBrowseError", () => {
75 it("derives a stable message from browse context while retaining the cause", () => {
76 const cause = new Error("sensitive filesystem detail");
77 const error = new FilesystemBrowseError({
78 cwd: "/workspace",
79 partialPath: "./src/mai",
80 failure: "read_directory_failed",
81 parentPath: "/workspace/src",
82 cause,
83 });
84​
85 expect(error.message).toBe("Failed to browse filesystem path './src/mai' from '/workspace'.");
86 expect(error.message).not.toContain(cause.message);
87 expect(error.cause).toBe(cause);
88 });
89​
90 it("round-trips the exact parent path and cwd when reading the directory fails", () => {
91 const cwd = "/tmp/t3-browse/project ";
92 const parentPath = "/tmp/t3-browse/missing ";
93 const error = new FilesystemBrowseError({
94 cwd,
95 partialPath: `${parentPath}/`,
96 failure: "read_directory_failed",
97 parentPath,
98 pathMissing: true,
99 });
100 const encoded = encodeBrowseError(error);
101​
102 expect(encoded.cwd).toBe(cwd);
103 expect(decodeBrowseError(encoded).cwd).toBe(cwd);
104 expect(encoded.parentPath).toBe(parentPath);
105 expect(decodeBrowseError(encoded).parentPath).toBe(parentPath);
106 });
107​
108 it("decodes legacy message-only errors during rolling upgrades", () => {
109 const error = decodeBrowseError({
110 _tag: "FilesystemBrowseError",
111 message: "Legacy filesystem browse failure.",
112 });
113​
114 expect(error.message).toBe("Legacy filesystem browse failure.");
115 expect(error.partialPath).toBeUndefined();
116 expect(error.failure).toBeUndefined();
117 });
118});