Files
piik/tests/app-assembly.test.ts
T
AAA f6b611978b fix(app): restore approved connections and refine shared playback
Preserve explicit App approval, expose playback and transfer progress, and keep shared identity and native window presentation coherent. Retain terminal fallback, cancel external input safely, and reject withdrawn UDP preparation before adoption.

Reuse exact Web artifacts across matched targets and extend the existing text participant with typed history, replies and reactions. Verified Rust/WASM/protocol checks, 1614 Web tests, Windows capture compilation and five external input lifecycle tests.
2026-11-16 07:03:38 -04:00

204 lines
10 KiB
TypeScript

import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it, vi } from "vitest";
import { recordChannelWeb, verifyChannelWeb } from "../scripts/channel-web-artifact.mjs";
const source = vi.hoisted(() => ({
revision: "a".repeat(40), built: false, finalState: "clean", output: "", machine: 0x3e,
cargoTarget: "", web: "", webBuilds: 0,
}));
// Exercise the actual assembler and final export. Only external build tools,
// Git observations and license generation are replaced by the fixture.
vi.mock("node:child_process", async (original) => ({
...await original<typeof import("node:child_process")>(),
execFileSync: (_command: string, args: string[]) => {
if (args[0] === "rev-parse") {
return source.built && source.finalState === "revision" ? "b".repeat(40) : source.revision;
}
if (args[0] === "status") {
return source.built && source.finalState === "dirty" ? " M README.md\n" : "";
}
throw new Error(`Unexpected Git observation: ${args.join(" ")}`);
},
spawnSync: (_command: string, args: string[]) => {
if (args[0] === "+1.91.0" && args[1] === "build") {
const executable = Buffer.alloc(64);
if (source.machine > 0xffff) {
executable.set([0xcf, 0xfa, 0xed, 0xfe]);
executable.writeUInt32LE(source.machine, 4);
} else {
executable.set([0x7f, 0x45, 0x4c, 0x46, 2, 1]);
executable.writeUInt16LE(source.finalState === "wrong-architecture"
? (source.machine === 0x3e ? 0xb7 : 0x3e) : source.machine, 18);
}
const directory = join(source.cargoTarget, args[args.indexOf("--target") + 1]!, "release");
mkdirSync(directory, { recursive: true });
writeFileSync(join(directory, "piik-channel-app"), executable);
source.built = true;
if (source.finalState === "output-created") {
mkdirSync(source.output);
writeFileSync(join(source.output, "user.txt"), "keep");
}
} else {
throw new Error(`Unexpected build operation: ${args.join(" ")}`);
}
return { status: 0, stdout: "", stderr: "" };
},
}));
vi.mock("node:fs", async (original) => {
const fs = await original<typeof import("node:fs")>();
return { ...fs, cpSync: (...args: Parameters<typeof fs.cpSync>) => {
if (String(args[0]).endsWith("channel-web")) {
return fs.cpSync(source.web, args[1], args[2]);
}
if (source.finalState === "copy") {
fs.writeFileSync(join(source.output, "partial"), "incomplete");
throw new Error("copy interrupted");
}
return fs.cpSync(...args);
} };
});
vi.mock("../scripts/package-licenses.mjs", () => ({ writeAppLicenseNotices: () => undefined }));
vi.mock("../scripts/build-channel-web.mjs", () => ({
buildChannelWeb: (root: string, options: { web?: string; version: string; revision: string }) => {
if (options.web) return verifyChannelWeb(options.web, options);
source.webBuilds++;
return join(root, "build", "channel-web");
},
}));
vi.mock("../scripts/release-version.mjs", () => ({ buildVersion: () => "development" }));
vi.mock("../scripts/app-package-targets.mjs", async (original) => {
const targets = await original<typeof import("../scripts/app-package-targets.mjs")>();
return { ...targets, appPackageTarget: (id: string) => {
const target = targets.appPackageTarget(id);
return target && { ...target, nodePlatform: process.platform, nodeArch: process.arch };
} };
});
it.each(["matched", "mismatched"])("App assembly consumes an existing shared Web build (%s)", async scenario => {
const root = mkdtempSync(join(tmpdir(), "piik-app-shared-web-")), output = join(root, "output");
const previousArguments = process.argv, stdout = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
source.web = join(root, "web"); source.output = output; source.cargoTarget = join(root, "target");
source.built = false; source.finalState = "clean"; source.machine = 0x3e; source.webBuilds = 0;
mkdirSync(join(source.web, "assets"), { recursive: true });
writeFileSync(join(source.web, "index.html"), '<script src="/assets/index-fixture.js"></script>');
writeFileSync(join(source.web, "assets/index-fixture.js"), source.revision);
writeFileSync(join(source.web, "assets/adapter.wasm"), Buffer.from([0, 97, 115, 109]));
writeFileSync(join(source.web, "third-party-licenses.txt"), "Fixture notices");
recordChannelWeb(source.web, { version: scenario === "matched" ? "development" : "v3.0.0-beta.31", revision: source.revision });
vi.stubEnv("CARGO_TARGET_DIR", source.cargoTarget);
try {
process.argv = [process.execPath, "assemble-app.mjs", output, "--target", "linux-amd64", "--web", source.web];
vi.resetModules();
const assembly = import("../scripts/assemble-app.mjs");
if (scenario === "matched") {
await assembly;
expect(readFileSync(join(output, "web/assets/adapter.wasm"))).toEqual(Buffer.from([0, 97, 115, 109]));
expect(source.built).toBe(true);
} else {
await expect(assembly).rejects.toThrow("Shared Web build identity mismatch");
expect(existsSync(output)).toBe(false);
expect(source.built).toBe(false);
}
expect(source.webBuilds).toBe(0);
} finally { process.argv = previousArguments; stdout.mockRestore(); vi.unstubAllEnvs(); rmSync(root, { recursive: true, force: true }); }
});
describe.each(["linux-amd64", "linux-arm64"])("App package source identity (%s)", (target) => {
it.each(["clean", "nested", "dirty", "revision", "copy", "existing", "output-created", "wrong-architecture"])("exports only complete owned output from unchanged source (%s)", async (finalState) => {
const root = mkdtempSync(join(tmpdir(), "piik-app-assembly-"));
const output = join(root, ...(finalState === "nested" ? ["new-parent"] : []), "app");
const previousArguments = process.argv;
const stdout = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
source.cargoTarget = join(root, "target");
source.web = join(root, "web");
mkdirSync(source.web);
writeFileSync(join(source.web, "index.html"), "fixture web");
vi.stubEnv("CARGO_TARGET_DIR", source.cargoTarget);
source.built = false;
source.finalState = finalState;
source.output = output;
source.machine = target === "linux-arm64" ? 0xb7 : 0x3e;
try {
if (finalState === "existing") {
mkdirSync(output);
writeFileSync(join(output, "user.txt"), "keep");
}
process.argv = [process.execPath, "assemble-app.mjs", output, "--target", target];
vi.resetModules();
const assembly = import("../scripts/assemble-app.mjs");
if (finalState === "clean" || finalState === "nested") {
await assembly;
expect(readFileSync(join(output, "REVISION"), "ascii")).toBe(`${source.revision}\n`);
expect(existsSync(join(output, "piik-app"))).toBe(true);
expect(readFileSync(join(output, "web", "index.html"), "utf8")).toBe("fixture web");
expect(existsSync(join(output, "runtime", "native"))).toBe(false);
} else if (finalState === "existing" || finalState === "output-created") {
await expect(assembly).rejects.toThrow(finalState === "existing" ? "must not already exist" : "EEXIST");
expect(readFileSync(join(output, "user.txt"), "utf8")).toBe("keep");
expect(existsSync(join(output, "piik-app"))).toBe(false);
} else if (finalState === "copy") {
await expect(assembly).rejects.toThrow("copy interrupted");
expect(existsSync(output)).toBe(false);
} else if (finalState === "wrong-architecture") {
await expect(assembly).rejects.toThrow(`App executable does not match App package target ${target}`);
expect(existsSync(output)).toBe(false);
} else {
await expect(assembly).rejects.toThrow(finalState === "dirty" ? "clean Git checkout" : "package source");
expect(existsSync(output)).toBe(false);
}
expect(source.built).toBe(finalState !== "existing");
} finally {
process.argv = previousArguments;
stdout.mockRestore();
vi.unstubAllEnvs();
rmSync(root, { recursive: true, force: true });
}
});
});
it("assembles a macOS bundle that retains its executable, frontend and tunnel when moved alone", async () => {
const root = mkdtempSync(join(tmpdir(), "piik-mac-bundle-"));
const output = join(root, "archive");
const previousArguments = process.argv;
const stdout = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
source.built = false;
source.finalState = "clean";
source.output = output;
source.machine = 0x0100000c;
source.cargoTarget = join(root, "target");
source.web = join(root, "web");
mkdirSync(source.web);
writeFileSync(join(source.web, "index.html"), "fixture web");
vi.stubEnv("CARGO_TARGET_DIR", source.cargoTarget);
try {
const helper = Buffer.alloc(64);
helper.set([0xcf, 0xfa, 0xed, 0xfe]);
helper.writeUInt32LE(source.machine, 4);
writeFileSync(join(root, "helper"), helper);
process.argv = [process.execPath, "assemble-app.mjs", output, "--target", "darwin-arm64",
"--tunnel", join(root, "helper")];
vi.resetModules();
await import("../scripts/assemble-app.mjs");
const relocated = join(root, "Moved Piik.app");
renameSync(join(output, "Piik App.app"), relocated);
const contents = join(relocated, "Contents");
const plist = readFileSync(join(contents, "Info.plist"), "utf8");
const entry = plist.match(/<key>CFBundleExecutable<\/key><string>([^<]+)<\/string>/)![1]!;
const executable = join(contents, "MacOS", entry);
expect(readFileSync(executable).subarray(0, 4)).toEqual(helper.subarray(0, 4));
expect(readFileSync(join(contents, "MacOS", "runtime/tunnel/cloudflared"))).toEqual(helper);
expect(readFileSync(join(contents, "MacOS", "web/index.html"), "utf8")).toBe("fixture web");
expect(existsSync(join(contents, "MacOS", "runtime/native"))).toBe(false);
expect(readFileSync(join(contents, "MacOS", "REVISION"), "ascii").trim()).toBe(source.revision);
expect(existsSync(join(output, "piik-app"))).toBe(false);
} finally {
process.argv = previousArguments;
stdout.mockRestore();
vi.unstubAllEnvs();
rmSync(root, { recursive: true, force: true });
}
});