mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 13:45:30 +00:00
96 lines
2.8 KiB
TypeScript
96 lines
2.8 KiB
TypeScript
import { isAbsolute, win32 } from "node:path";
|
|
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
import { z } from "zod";
|
|
|
|
import { expect, it } from "vitest";
|
|
|
|
import {
|
|
absoluteLocalPathStringSchema,
|
|
windowsAbsolutePathPattern,
|
|
} from "./localPath.js";
|
|
|
|
const PATHS = [
|
|
"/absolute/path",
|
|
"/",
|
|
"relative/path",
|
|
"C:\\absolute\\path",
|
|
"c:/absolute/path",
|
|
"C:relative/path",
|
|
"\\\\server\\share\\path",
|
|
"\\rooted\\path",
|
|
"//server/share/path",
|
|
"file:///absolute/path",
|
|
];
|
|
|
|
it("serializes portable path constraints for catalogs shipped to another host", () => {
|
|
const ajv = new Ajv2020({ strict: false });
|
|
for (const io of ["input", "output"] as const) {
|
|
const schema = JSON.parse(
|
|
JSON.stringify(z.toJSONSchema(absoluteLocalPathStringSchema, { io })),
|
|
);
|
|
const validate = ajv.compile(schema);
|
|
for (const path of PATHS.filter((path) => win32.isAbsolute(path)))
|
|
expect(validate(path), `${io}: ${path}`).toBe(true);
|
|
for (const path of ["", "relative/path", "C:relative/path", "/tmp/\0"])
|
|
expect(validate(path), `${io}: ${path}`).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("matches the host path module for absolute local paths", () => {
|
|
const checked: string[] = [];
|
|
for (const path of PATHS) {
|
|
checked.push(path);
|
|
expect(absoluteLocalPathStringSchema.safeParse(path).success).toBe(
|
|
isAbsolute(path),
|
|
);
|
|
}
|
|
expect(checked).toEqual(
|
|
expect.arrayContaining([
|
|
"/absolute/path",
|
|
"relative/path",
|
|
"file:///absolute/path",
|
|
]),
|
|
);
|
|
expect(absoluteLocalPathStringSchema.safeParse("").success).toBe(false);
|
|
expect(
|
|
absoluteLocalPathStringSchema.safeParse("/absolute/with\0nul").success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("keeps the Windows absolute-path pattern valid in every client mode", () => {
|
|
// Annex B and `u` accept an unescaped `/` inside a class; `v` does not, and a
|
|
// client that compiles advertised patterns rejects the complete tools request.
|
|
for (const flags of ["", "u", "v"])
|
|
expect(
|
|
() => new RegExp(windowsAbsolutePathPattern.source, flags),
|
|
flags,
|
|
).not.toThrow();
|
|
const checked: string[] = [];
|
|
for (const path of PATHS) {
|
|
checked.push(path);
|
|
expect(windowsAbsolutePathPattern.test(path), path).toBe(
|
|
win32.isAbsolute(path),
|
|
);
|
|
}
|
|
expect(checked).toEqual(
|
|
expect.arrayContaining([
|
|
"C:\\absolute\\path",
|
|
"C:relative/path",
|
|
"/absolute/path",
|
|
]),
|
|
);
|
|
});
|
|
|
|
it("rejects unpaired surrogates while preserving representable Unicode paths", () => {
|
|
for (const path of ["/tmp/\ud800", "/tmp/\udc00", "/tmp/\ud800x"]) {
|
|
expect(absoluteLocalPathStringSchema.safeParse(path).success, path).toBe(
|
|
false,
|
|
);
|
|
}
|
|
for (const path of ["/tmp/😀", "/tmp/�", "/tmp/ spaced name "]) {
|
|
expect(absoluteLocalPathStringSchema.safeParse(path).success, path).toBe(
|
|
true,
|
|
);
|
|
}
|
|
});
|