1 +import assert from "node:assert/strict";
2 +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3 +import { tmpdir } from "node:os";
4 +import { join } from "node:path";
5 +import { test } from "node:test";
6 +import { __test as code } from "./gh_code_search.mjs";
7 +import { __test as failed } from "./gh_failed_run.mjs";
8 +import { __test as pages } from "./gh_pages_deploy.mjs";
10 +function withArgv(args, fn) {
11 + const old = process.argv;
12 + process.argv = [process.execPath, "script.mjs", ...args];
20 +function responseSequence(...responses) {
21 + return () => responses.shift() ?? { status: 1, stdout: "", stderr: "unexpected call" };
24 +function withoutConsole(fn) {
25 + const oldLog = console.log;
26 + const oldError = console.error;
27 + console.log = () => {};
28 + console.error = () => {};
32 + console.log = oldLog;
33 + console.error = oldError;
37 +test("code search builds gh args from CLI flags", () => {
49 + assert.equal(code.positional(), "TODO");
50 + assert.deepEqual(code.build("TODO"), [
64 + "path,repository,sha,textMatches,url",
69 +test("code search filters, sorts, and formats results", () => {
71 + { path: "b.js", repository: { nameWithOwner: "z/repo", isFork: true }, textMatches: [{ fragment: "b" }], url: "u2" },
72 + { path: "a.js", repository: { nameWithOwner: "a/repo", isPrivate: true }, textMatches: [{ fragment: "a" }, { fragment: "aa" }], url: "u1" },
74 + assert.equal(withArgv(["q", "--exclude-forks"], () => code.filter(results)).length, 1);
75 + assert.equal(withArgv(["q", "--exclude-private"], () => code.filter(results)).length, 1);
76 + assert.equal(withArgv(["q", "--min-matches", "2"], () => code.filter(results))[0].path, "a.js");
77 + assert.equal(code.sort(results, "repo")[0].path, "a.js");
78 + assert.equal(code.sort(results, "path")[0].path, "a.js");
79 + assert.equal(code.sort(results, "matches")[0].path, "a.js");
80 + assert.match(code.pretty(results), /Found 2 result/);
81 + assert.match(code.summary(results), /Total files found: 2/);
82 + assert.equal(code.pretty([]), "No results found.");
85 +test("failed run helpers clean logs and target jobs by database ID", () => {
86 + assert.equal(failed.clean("\u001b[31merror\u001b[0m\r"), "error");
87 + assert.deepEqual(failed.repoArgs("o/r"), ["--repo", "o/r"]);
88 + assert.deepEqual(failed.repoArgs(), []);
89 + assert.deepEqual(failed.jobLogArgs(42, 9001, "o/r"), [
99 + const out = failed.excerpts("##[command] npm test\nError: boom\nError: boom\nok\nProcess completed with exit code 1", 5);
100 + assert.deepEqual(out, ["Error: boom", "Process completed with exit code 1"]);
103 +test("pages workflow requires an explicit site directory", () => {
105 + () => pages.createWorkflow(".github/workflows/pages.yml", false, false),
110 +test("pages workflow dry-run never writes or overwrites", () => {
111 + const dir = mkdtempSync(join(tmpdir(), "gh-pages-test-"));
112 + const output = join(dir, "pages.yml");
113 + const oldLog = console.log;
114 + console.log = () => {};
116 + const preview = pages.createWorkflow(output, false, true, "dist");
117 + assert.equal(preview.status, "dry_run");
118 + assert.equal(existsSync(output), false);
119 + writeFileSync(output, "keep\n");
120 + const replacement = pages.createWorkflow(output, true, true, "dist");
121 + assert.equal(replacement.status, "dry_run");
122 + assert.equal(readFileSync(output, "utf8"), "keep\n");
124 + console.log = oldLog;
125 + rmSync(dir, { recursive: true, force: true });
129 +test("pages helper creates a workflow for only the explicit site directory", () => {
130 + assert.equal(pages.msg("done", "success"), "✓ done");
131 + assert.equal(pages.json({ ok: true }), '{\n "ok": true\n}');
132 + assert.throws(() => pages.validateSiteDir("../private"), /repository-relative/);
133 + const dir = mkdtempSync(join(tmpdir(), "gh-pages-test-"));
134 + const output = join(dir, ".github/workflows/pages.yml");
135 + const oldLog = console.log;
137 + console.log = (line = "") => logs.push(String(line));
139 + const created = pages.createWorkflow(output, false, false, "dist");
140 + assert.equal(created.status, "created");
141 + const workflow = readFileSync(output, "utf8");
142 + assert.match(workflow, /actions\/deploy-pages@v4/);
143 + assert.match(workflow, /path: 'dist'/);
144 + assert.doesNotMatch(workflow, /cp -r/);
145 + const skipped = pages.createWorkflow(output, false, false, "dist");
146 + assert.equal(skipped.status, "skipped");
147 + assert.match(logs.join("\n"), /file_exists/);
149 + console.log = oldLog;
150 + rmSync(dir, { recursive: true, force: true });
154 +test("pages status preserves API failures as errors", () => {
155 + const run = responseSequence(
156 + { status: 0, stdout: "", stderr: "" },
157 + { status: 1, stdout: "", stderr: "permission denied" },
159 + const result = withoutConsole(() => new pages.Pages("o/r", false, true, run).status());
160 + assert.deepEqual(result, {
164 + error: "permission denied",
168 +test("pages enable reports failed HTTPS enforcement", () => {
169 + const run = responseSequence(
170 + { status: 0, stdout: "", stderr: "" },
171 + { status: 0, stdout: "{}", stderr: "" },
172 + { status: 1, stdout: "", stderr: "HTTPS unavailable" },
174 + const result = withoutConsole(() => new pages.Pages("o/r", false, true, run).enable());
175 + assert.deepEqual(result, {
178 + action: "enforce_https",
179 + pages_enabled: true,
180 + error: "HTTPS unavailable",