Initial release of File Tools 0.1.0
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { expect, test, type Page, type Response } from "@playwright/test";
|
||||
|
||||
const ORIGIN = "http://127.0.0.1:4173";
|
||||
async function localOnly(page: Page) {
|
||||
const external: string[] = [];
|
||||
await page.route("**/*", async (route) => {
|
||||
const url = new URL(route.request().url());
|
||||
if (url.origin !== ORIGIN) {
|
||||
external.push(url.href);
|
||||
await route.abort();
|
||||
} else await route.continue();
|
||||
});
|
||||
return external;
|
||||
}
|
||||
|
||||
test("runs from a nested path without external requests", async ({ page }) => {
|
||||
const errors: string[] = [];
|
||||
page.on("pageerror", (error) => errors.push(error.message));
|
||||
page.on("console", (message) => {
|
||||
if (message.type() === "error") errors.push(message.text());
|
||||
});
|
||||
const external = await localOnly(page);
|
||||
await page.goto("/deep/nested/file/");
|
||||
await expect(page.getByRole("heading", { name: "File Tools" })).toBeVisible();
|
||||
expect(external).toEqual([]);
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
test("inspects and hashes a local file in an immutable module worker", async ({
|
||||
page,
|
||||
}) => {
|
||||
const external = await localOnly(page);
|
||||
const responses: Response[] = [];
|
||||
page.on("response", (response) => responses.push(response));
|
||||
await page.goto("/deep/nested/file/");
|
||||
await page
|
||||
.locator('input[type="file"]')
|
||||
.first()
|
||||
.setInputFiles("tests/fixtures/hello.txt");
|
||||
await expect(page.getByRole("heading", { name: "hello.txt" })).toBeVisible();
|
||||
await expect(
|
||||
page.getByText("text/plain (.txt)", { exact: true }),
|
||||
).toBeVisible();
|
||||
await page.getByRole("button", { name: "Hash", exact: true }).click();
|
||||
await expect(page.getByText(/^[0-9a-f]{64}$/u)).toBeVisible();
|
||||
const worker = responses.find((response) =>
|
||||
/\/deep\/nested\/file\/assets\/inspect\.worker-[\w-]+\.js$/u.test(
|
||||
new URL(response.url()).pathname,
|
||||
),
|
||||
);
|
||||
expect(worker, "inspection worker request").toBeTruthy();
|
||||
expect(worker!.headers()["content-type"]).toContain("text/javascript");
|
||||
expect(worker!.headers()["cache-control"]).toBe(
|
||||
"public, max-age=31536000, immutable",
|
||||
);
|
||||
expect(new URL(worker!.url()).origin).toBe(ORIGIN);
|
||||
expect(external).toEqual([]);
|
||||
});
|
||||
|
||||
test("serves the release identity and hardened headers", async ({
|
||||
request,
|
||||
}) => {
|
||||
const index = await request.get("/deep/nested/file/");
|
||||
expect(index.ok()).toBe(true);
|
||||
expect(index.headers()["content-security-policy"]).toContain(
|
||||
"default-src 'self'",
|
||||
);
|
||||
expect(await index.text()).not.toMatch(/\b(?:src|href)=["']\//u);
|
||||
const manifest = await request.get("/deep/nested/file/toolbox-app.json");
|
||||
await expect(manifest.json()).resolves.toMatchObject({
|
||||
id: "de.add-ideas.file-tools",
|
||||
version: "0.1.0",
|
||||
entry: "./",
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { App } from "../../src/App";
|
||||
|
||||
describe("File Tools", () => {
|
||||
it("renders the local workbench and standard shell", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => new Response("Not found", { status: 404 })),
|
||||
);
|
||||
render(<App />);
|
||||
expect(
|
||||
await screen.findByRole("heading", { name: "File Tools" }),
|
||||
).toBeVisible();
|
||||
expect(await screen.findByText("Browser-local")).toBeVisible();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { manifestCsv, manifestJson } from "../../src/file/manifest";
|
||||
import {
|
||||
detectKnownSignature,
|
||||
extensionMismatch,
|
||||
} from "../../src/file/signatures";
|
||||
import {
|
||||
extractStrings,
|
||||
looksLikeText,
|
||||
shannonEntropy,
|
||||
} from "../../src/file/text";
|
||||
|
||||
describe("file signatures", () => {
|
||||
it("detects PNG without claiming parser validation", () =>
|
||||
expect(
|
||||
detectKnownSignature(
|
||||
Uint8Array.of(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a),
|
||||
),
|
||||
).toMatchObject({ extension: "png", mime: "image/png" }));
|
||||
it("accounts for container extension aliases", () => {
|
||||
expect(extensionMismatch("book.epub", "zip")).toBeUndefined();
|
||||
expect(extensionMismatch("image.txt", "png")).toMatch(/does not match/u);
|
||||
});
|
||||
});
|
||||
|
||||
describe("bounded text helpers", () => {
|
||||
it("extracts ASCII and UTF-16LE strings", () =>
|
||||
expect(
|
||||
extractStrings(
|
||||
Uint8Array.from([65, 66, 67, 68, 0, 90, 0, 89, 0, 88, 0, 87, 0]),
|
||||
),
|
||||
).toEqual(expect.arrayContaining(["ABCD", "ZYXW"])));
|
||||
it("distinguishes NUL-containing binary data", () =>
|
||||
expect(looksLikeText(Uint8Array.of(65, 0, 66))).toBe(false));
|
||||
it("measures a uniform byte distribution", () =>
|
||||
expect(
|
||||
shannonEntropy(Uint8Array.from({ length: 256 }, (_, index) => index)),
|
||||
).toBeCloseTo(8));
|
||||
});
|
||||
|
||||
describe("manifests", () => {
|
||||
const records = [
|
||||
{
|
||||
name: "=formula",
|
||||
path: "=formula",
|
||||
size: 1,
|
||||
type: "text/plain",
|
||||
lastModified: 0,
|
||||
},
|
||||
];
|
||||
it("is deterministic", () =>
|
||||
expect(manifestJson(records)).toBe(manifestJson(records)));
|
||||
it("neutralises spreadsheet-leading formulas", () =>
|
||||
expect(manifestCsv(records)).toContain("'=formula"));
|
||||
});
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
Hello local file
|
||||
Reference in New Issue
Block a user