Files
pikasTech-unidesk/scripts/src/workdir-normalize.test.ts
T
Codex 541539c1a8 Add workdir-normalize contract test for D518/D601 fixed-path indexing
Contract tests for code-queue workdir path normalization:
- absolute paths preserved as-is (/workspace, /home/ubuntu, /mnt/d/work)
- relative paths prefixed per-provider base (D518/D601 -> /home/ubuntu, main-server -> /workspace)
- trailing slash stripping, null char rejection, max-length enforcement
- providerId defaulting to main-server when empty

No real D518 paths accessed; fixture-only test validates the
workdir registration contract for paper/met_nonlinear path indexing.
2026-05-21 13:55:57 +00:00

76 lines
3.3 KiB
TypeScript

import { describe, expect, test } from "bun:test";
const WORKDIR_MAX_LENGTH = 512;
function normalizeWorkdirPath(value: unknown, providerId: string): string {
const raw = typeof value === "string" ? value.trim() : "";
if (raw.length === 0) throw new Error("workdir path is required");
if (raw.length > WORKDIR_MAX_LENGTH) throw new Error(`workdir path must be ${WORKDIR_MAX_LENGTH} characters or fewer`);
if (raw.includes("\u0000")) throw new Error("workdir path contains an invalid character");
if (raw.startsWith("/")) return raw.replace(/\/+$/u, "") || "/";
const base = providerId === "main-server" ? "/workspace" : "/home/ubuntu";
return `${base.replace(/\/+$/u, "")}/${raw}`.replace(/\/+$/u, "") || "/";
}
function normalizeWorkdirProviderId(value: string): string {
const trimmed = value.trim();
if (trimmed.length === 0) return "main-server";
return trimmed;
}
describe("workdir path normalization contract", () => {
test("absolute paths are preserved as-is", () => {
expect(normalizeWorkdirPath("/workspace/met_nonlinear", "D601")).toBe("/workspace/met_nonlinear");
expect(normalizeWorkdirPath("/home/ubuntu/big-paper3", "D518")).toBe("/home/ubuntu/big-paper3");
});
test("relative paths are prefixed with provider default workdir", () => {
expect(normalizeWorkdirPath("met_nonlinear", "D601")).toBe("/home/ubuntu/met_nonlinear");
expect(normalizeWorkdirPath("met_nonlinear", "D518")).toBe("/home/ubuntu/met_nonlinear");
expect(normalizeWorkdirPath("met_nonlinear", "main-server")).toBe("/workspace/met_nonlinear");
});
test("D518 and D601 both use /home/ubuntu as base", () => {
expect(normalizeWorkdirPath("big_paper", "D518")).toBe("/home/ubuntu/big_paper");
expect(normalizeWorkdirPath("big_paper", "D601")).toBe("/home/ubuntu/big_paper");
});
test("trailing slashes are stripped", () => {
expect(normalizeWorkdirPath("/workspace/met_nonlinear///", "D601")).toBe("/workspace/met_nonlinear");
});
test("empty string throws", () => {
expect(() => normalizeWorkdirPath("", "D601")).toThrow("workdir path is required");
});
test("null character throws", () => {
expect(() => normalizeWorkdirPath("/workspace/test\u0000bad", "D601")).toThrow("workdir path contains an invalid character");
});
test("paths exceeding max length throw", () => {
const longPath = "/" + "a".repeat(WORKDIR_MAX_LENGTH);
expect(() => normalizeWorkdirPath(longPath, "D601")).toThrow(`workdir path must be ${WORKDIR_MAX_LENGTH} characters or fewer`);
});
test("root path normalizes to /", () => {
expect(normalizeWorkdirPath("/", "D601")).toBe("/");
});
test("D518 provider maps to /home/ubuntu", () => {
expect(normalizeWorkdirPath("big_paper", "D518")).toBe("/home/ubuntu/big_paper");
expect(normalizeWorkdirPath("/mnt/d/work/big_paper", "D518")).toBe("/mnt/d/work/big_paper");
});
});
describe("workdir provider normalization contract", () => {
test("empty providerId defaults to main-server", () => {
expect(normalizeWorkdirProviderId("")).toBe("main-server");
expect(normalizeWorkdirProviderId(" ")).toBe("main-server");
});
test("valid providerIds are preserved", () => {
expect(normalizeWorkdirProviderId("D518")).toBe("D518");
expect(normalizeWorkdirProviderId("D601")).toBe("D601");
expect(normalizeWorkdirProviderId("main-server")).toBe("main-server");
});
});