craftz

The generator's test

Writes a package, a site, a package with a site, scripts in a private repository and an internal site in a public one, the way pnpm new does. Each is checked against the survey and runs its verify. If the rules change and the generator doesn't follow, this fails here, before a new repository starts out of line.

import { execFileSync } from "node:child_process";
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, test } from "vite-plus/test";
import { type Start, write } from "../src/new.ts";
import { checkFolder } from "../src/survey.ts";

const starts: [string, Start][] = [
  ["a package", { package: true, site: false, private: false, internal: false }],
  ["a static site", { package: false, site: true, private: false, internal: false }],
  ["a package with a site", { package: true, site: true, private: false, internal: false }],
  [
    "scripts in a private repository",
    { package: false, site: false, private: true, internal: false },
  ],
  ["an internal site", { package: false, site: true, private: false, internal: true }],
];

for (const [what, start] of starts) {
  test(`${what} follows the rules and passes verify`, { timeout: 300_000 }, async () => {
    const dir = join(mkdtempSync(join(tmpdir(), "craftz-new-")), "example");
    await write(dir, "example", start, "An example, from craftz's test.");
    const { findings, worker } = await checkFolder(dir);
    expect(findings.flatMap((f) => f.drift)).toEqual([]);
    // Every site answers only on its domain, internal or not.
    if (worker) expect([worker.workersDev, worker.previewUrls]).toEqual([false, false]);
    expect(worker?.deploys).toBe(start.site ? "static" : undefined);
    const run = (...args: string[]) => execFileSync("pnpm", args, { cwd: dir, stdio: "pipe" });
    run("install");
    run("run", "fix");
    run("run", "verify");
  });
}