repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
pi/agent/skills/gh-cli/scripts/gh-cli.test.mjs
RawFile outline
import assert from "node:assert/strict";
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "node:test";
import { __test as code } from "./gh_code_search.mjs";
import { __test as failed } from "./gh_failed_run.mjs";
import { __test as pages } from "./gh_pages_deploy.mjs";
function withArgv(args, fn) {
const old = process.argv;
process.argv = [process.execPath, "script.mjs", ...args];
try {
return fn();
} finally {
process.argv = old;
}
}
test("code search builds gh args from CLI flags", () => {
withArgv([
"TODO",
"--limit",
"5",
"--language",
"javascript",
"--repo",
"o/r",
"--owner",
"org",
], () => {
assert.equal(code.positional(), "TODO");
assert.deepEqual(code.build("TODO"), [
"gh",
"search",
"code",
"TODO",
"--limit",
"5",
"--language",
"javascript",
"--repo",
"o/r",
"--owner",
"org",
"--json",
"path,repository,sha,textMatches,url",
]);
});
});
test("code search filters, sorts, and formats results", () => {
const results = [
{ path: "b.js", repository: { nameWithOwner: "z/repo", isFork: true }, textMatches: [{ fragment: "b" }], url: "u2" },
{ path: "a.js", repository: { nameWithOwner: "a/repo", isPrivate: true }, textMatches: [{ fragment: "a" }, { fragment: "aa" }], url: "u1" },
];
assert.equal(withArgv(["q", "--exclude-forks"], () => code.filter(results)).length, 1);
assert.equal(withArgv(["q", "--exclude-private"], () => code.filter(results)).length, 1);
assert.equal(withArgv(["q", "--min-matches", "2"], () => code.filter(results))[0].path, "a.js");
assert.equal(code.sort(results, "repo")[0].path, "a.js");
assert.equal(code.sort(results, "path")[0].path, "a.js");
assert.equal(code.sort(results, "matches")[0].path, "a.js");
assert.match(code.pretty(results), /Found 2 result/);
assert.match(code.summary(results), /Total files found: 2/);
assert.equal(code.pretty([]), "No results found.");
});
test("failed run helpers clean logs and extract excerpts", () => {
assert.equal(failed.clean("\u001b[31merror\u001b[0m\r"), "error");
assert.deepEqual(failed.repoArgs("o/r"), ["--repo", "o/r"]);
assert.deepEqual(failed.repoArgs(), []);
const out = failed.excerpts("##[command] npm test\nError: boom\nError: boom\nok\nProcess completed with exit code 1", 5);
assert.deepEqual(out, ["Error: boom", "Process completed with exit code 1"]);
});
test("pages helper formats messages and creates workflow safely", () => {
assert.equal(pages.msg("done", "success"), "✓ done");
assert.equal(pages.json({ ok: true }), '{\n "ok": true\n}');
const dir = mkdtempSync(join(tmpdir(), "gh-pages-test-"));
const oldCwd = process.cwd();
const oldLog = console.log;
const logs = [];
console.log = (line = "") => logs.push(String(line));
try {
process.chdir(dir);
const created = pages.createWorkflow(".github/workflows/pages.yml", false);
assert.equal(created.status, "created");
assert.match(readFileSync(join(dir, ".github/workflows/pages.yml"), "utf8"), /actions\/deploy-pages@v4/);
const skipped = pages.createWorkflow(".github/workflows/pages.yml", false);
assert.equal(skipped.status, "skipped");
assert.match(logs.join("\n"), /file_exists/);
} finally {
console.log = oldLog;
process.chdir(oldCwd);
rmSync(dir, { recursive: true, force: true });
}
});