// Tests for Syndria98/dragonroll#29: the monorepo is a pnpm workspace, so running or installing it // goes through pnpm (`pnpm install`, `pnpm dev`, `pnpm test`) and the images are built with the // workspace manifests instead of one npm lockfile per package. // // These checks only read the manifests, the Dockerfiles and the workflows, so they need no installed // dependencies. They fail when a package is added to the repository without joining the workspace, or // when a build step slips back to npm and its deleted `package-lock.json`. // // Run with `pnpm test` from the repository root, or `node --test tests/`. const { test } = require("node:test"); const assert = require("node:assert/strict"); const fs = require("node:fs"); const path = require("node:path"); const root = path.join(__dirname, ".."); const read = (...parts) => fs.readFileSync(path.join(root, ...parts), "utf8"); const workspace = read("pnpm-workspace.yaml"); const rootManifest = JSON.parse(read("package.json")); // `dragonroll-foundry/bridge` is deliberately left out: it depends on nothing but Node built-ins, it // is not part of `pnpm dev`, and its README runs it with `npm start --prefix foundry/bridge`. const apps = ["backend", "frontend", "discord-bot"]; function packageJsonOf(pkg) { return JSON.parse(read(pkg, "package.json")); } // The items of a top-level `key:` list of pnpm-workspace.yaml, quotes dropped. function yamlList(key) { const block = new RegExp(`${key}\\n((?:\\s+-.*\\n?)+)`).exec(workspace)?.[1] ?? ""; return [...block.matchAll(/^\s+-\s+'?([^'\s]+)'?\s*$/gm)].map((match) => match[1]); } test("every app package of the monorepo belongs to the pnpm workspace", () => { const listed = yamlList("packages:"); for (const pkg of apps) { assert.ok(listed.includes(pkg), `${pkg} must be listed in pnpm-workspace.yaml`); assert.ok(fs.existsSync(path.join(root, pkg, "package.json")), `${pkg} must have a package.json`); } }); test("the workspace root is private and pins one pnpm version for corepack", () => { assert.equal(rootManifest.private, true, "the workspace root must not be publishable"); assert.match(rootManifest.packageManager, /^pnpm@\d+\.\d+\.\d+$/, "packageManager must pin pnpm"); }); test("npm lockfiles stay gone and pnpm-lock.yaml is the only lockfile", () => { const lockfiles = ["", ...apps].flatMap((pkg) => [ path.join(root, pkg, "package-lock.json"), path.join(root, pkg, "yarn.lock"), ]); const present = lockfiles.filter((lockfile) => fs.existsSync(lockfile)); assert.deepEqual(present, [], `only pnpm-lock.yaml may exist, found ${present.join(", ")}`); assert.ok(fs.existsSync(path.join(root, "pnpm-lock.yaml")), "pnpm-lock.yaml must be committed"); }); test("`pnpm dev` runs the whole monorepo through the workspace instead of npm --prefix", () => { const dev = rootManifest.scripts.dev; assert.match(dev, /^concurrently -k/, "the dev script keeps running every app together"); assert.ok(!/\bnpm\b/.test(dev), "the dev script must not call npm"); for (const pkg of apps) { assert.ok( dev.includes(`pnpm --filter ./${pkg}`), `the dev script must run ${pkg} through the workspace: ${dev}` ); } // The Foundry launcher is not a package, so concurrently keeps driving it. assert.match(dev, /node scripts\/run-foundry\.js/); }); test("every app package can be started on its own", () => { // `pnpm dev:`; building stays `pnpm --filter ./frontend run build`. for (const pkg of apps) { const script = rootManifest.scripts[`dev:${pkg}`]; assert.ok(script, `the root needs a dev:${pkg} script`); assert.match(script, new RegExp(`pnpm --filter ./${pkg} run \\w+`)); const packageScript = packageJsonOf(pkg).scripts[/run (\w+)$/.exec(script)[1]]; assert.ok(packageScript, `${pkg} must define the script dev:${pkg} runs`); } }); test("the Dockerfiles install with pnpm from the workspace root", () => { for (const pkg of apps) { const dockerfile = read(pkg, "Dockerfile"); assert.match(dockerfile, /corepack enable/, `${pkg}: pnpm must come from corepack`); assert.match( dockerfile, /COPY pnpm-workspace\.yaml pnpm-lock\.yaml package.json/, `${pkg}: the workspace manifests must be copied before installing` ); assert.match( dockerfile, new RegExp(`pnpm install --frozen-lockfile[^\\n]*--filter \\./${pkg}`), `${pkg}: dependencies must be installed from the lockfile, filtered to this package` ); assert.ok(!/\bnpm (ci|install)\b/.test(dockerfile), `${pkg}: must not fall back to npm install`); } // The images are built from the repository root, so the manifests are in the context. const deploy = read(".gitea", "workflows", "deploy.yml"); for (const pkg of apps) { assert.ok( deploy.includes(`-f ./${pkg}/Dockerfile .`), `${pkg} must be built with the repository root as build context` ); } }); test("the test workflow runs the suite through pnpm", () => { assert.match(read(".gitea", "workflows", "test.yml"), /corepack pnpm test/); }); test("the workspace allows the dependencies that build native binaries to run their scripts", () => { // pnpm skips install scripts of dependencies by default; the frontend build needs these two to // place their native binary. const allowed = yamlList("onlyBuiltDependencies:"); for (const dependency of ["esbuild", "@parcel/watcher"]) { assert.ok(allowed.includes(dependency), `${dependency} must be listed in onlyBuiltDependencies`); } });