mvp-ai's picture
download
raw
5.82 kB
// Copyright (c) 2026 the Zuke contributors
// SPDX-License-Identifier: MIT
/** Test doubles for the CLI's injectable seams. */
import type { SetupHost } from "../src/setup.ts";
import type { BuildProbe, Prompter } from "../mod.ts";
/** An in-memory {@link SetupHost} that records writes, chmods, and logs. */
export class FakeHost implements SetupHost {
/** Virtual filesystem: path → contents. */
readonly files = new Map<string, string>();
/** Paths that exist as directories (for reserved-name collision tests). */
readonly directories = new Set<string>();
/** Paths that exist as symbolic links (for the refuse-to-follow tests). */
readonly symlinks = new Set<string>();
/** Lines passed to {@link log}. */
readonly logs: string[] = [];
/** `[path, mode]` pairs passed to {@link chmod}. */
readonly chmods: Array<[string, number]> = [];
/** Paths passed to {@link readText}, so a test can assert what was *not*
* read — setup refusing to open a file outside its directory. */
readonly reads: string[] = [];
/** When true, {@link chmod} rejects (simulating an unsupported platform). */
chmodFails = false;
/** What {@link isTerminal} answers: off by default, as under `deno test`. */
terminal = false;
constructor(initial?: Record<string, string>) {
if (initial) {
for (const [path, content] of Object.entries(initial)) {
this.files.set(path, content);
}
}
}
exists(path: string): Promise<boolean> {
return Promise.resolve(
this.files.has(path) || this.directories.has(path) ||
this.symlinks.has(path),
);
}
isDirectory(path: string): Promise<boolean> {
return Promise.resolve(this.directories.has(path));
}
isSymlink(path: string): Promise<boolean> {
return Promise.resolve(this.symlinks.has(path));
}
readText(path: string): Promise<string> {
this.reads.push(path);
const value = this.files.get(path);
return value === undefined
? Promise.reject(new Error(`missing: ${path}`))
: Promise.resolve(value);
}
writeText(path: string, content: string): Promise<void> {
this.files.set(path, content);
return Promise.resolve();
}
chmod(path: string, mode: number): Promise<void> {
if (this.chmodFails) return Promise.reject(new Error("chmod unsupported"));
this.chmods.push([path, mode]);
return Promise.resolve();
}
log(message: string): void {
this.logs.push(message);
}
isTerminal(): boolean {
return this.terminal;
}
}
/** A scripted {@link Prompter} with canned answers. */
export class FakePrompter implements Prompter {
/** Every question passed to {@link confirm}, in order. */
readonly confirms: string[] = [];
/** Every question passed to {@link ask}, in order. */
readonly asks: string[] = [];
constructor(
private readonly tty: boolean,
private readonly answer: string = "",
private readonly yes: boolean = false,
/** The answer to the star prompt (kept apart from `yes`, so tests of the
* other confirms don't accidentally opt in to starring). */
private readonly star: boolean = false,
/** The answer to the launcher question; `""` takes its default. Kept
* apart from `answer`, which is the build class name. */
private readonly bootstrap: string = "",
) {}
interactive(): boolean {
return this.tty;
}
ask(question: string, fallback: string): string {
this.asks.push(question);
// Match the launcher question narrowly, as `confirm` does for the star
// one, so the class-name answer never leaks into a yes/no question.
if (question.includes("bootstrap a pinned")) {
return this.bootstrap === "" ? fallback : this.bootstrap;
}
return this.answer === "" ? fallback : this.answer;
}
confirm(question: string): boolean {
this.confirms.push(question);
// Match the star question narrowly: a broader match could silently route
// an unrelated confirm to `star` and let a test reach the real actions.
return question.includes("Star zuke-build/zuke") ? this.star : this.yes;
}
}
/** A scripted `StarActions` recording which effects ran. */
export class FakeStarActions {
/** The effect names invoked, in order. */
readonly calls: string[] = [];
constructor(
/** What `ghAuthenticated()` reports. */
private readonly authed: boolean,
/** Whether `starWithGh()` succeeds (else it rejects). */
private readonly starOk: boolean = true,
/** What `openBrowser()` reports. */
private readonly browserOk: boolean = true,
) {}
ghAuthenticated(): Promise<boolean> {
this.calls.push("auth");
return Promise.resolve(this.authed);
}
starWithGh(): Promise<void> {
this.calls.push("star");
return this.starOk
? Promise.resolve()
: Promise.reject(new Error("gh api failed"));
}
openBrowser(url: string): Promise<boolean> {
this.calls.push(`open:${url}`);
return Promise.resolve(this.browserOk);
}
}
/**
* A {@link BuildProbe} that finds no project, whatever the directory.
*
* `zuke --help` now shows the build's half too, which means a help test given
* the real probe locates the repository's own `zuke.json` and spawns its build
* — a real subprocess, in a test that only wanted to read text. That makes the
* test no longer hermetic, and it was caught by the coverage gate rather than by a failing
* assertion: the spawned build's module body landed in the profile and dragged
* `zuke.ts` under the per-file floor.
*
* A help test that is not about forwarding should use this.
*/
export const noProjectProbe: BuildProbe = {
exists: () => Promise.resolve(false),
ownership: () => Promise.resolve(null),
realPath: (path) => Promise.resolve(path),
readLink: () => Promise.resolve(null),
uid: () => null,
};

Xet Storage Details

Size:
5.82 kB
·
Xet hash:
f213ae594dc17ff6bf35f6a6bfe3d3eb0d738ba8433a50023bfd1154d9c41e5f

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.