Cotton code
77 lines · 4.2 KB · TypeScriptRaw
// Which branch of the device disk policy a fixture runs, said by the fixture.//// On a Mac every build and reclaim wrapper (with-build-lock, build-slot run,// disk-guard, the cargo sweep, require-free-disk) hands its admission to// scripts/lib/mac_disk_policy.py, which refuses to start until machine-row.sh has// published the device's keep-free reading to ~/.cottondb/disk-policy.json.// A fixture's scratch HOME has no app, so a fixture that runs the Mac branch// publishes the reading itself; one that tests the Linux body (the df tiers,// the sweep's rules, the slot budget) says so with a `uname` that answers Linux,// so the body under test runs on every host instead of only on Linux CI.// Guard: scripts/__tests__/disk-reading-fixture.test.ts.import { chmodSync, mkdirSync, mkdtempSync, statSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";/** Named in the hint a forgotten reading earns (scripts/__tests__/setup-disk-reading.ts). */export const DISK_READING_HELPER = "publishDiskReading(home) from scripts/__tests__/helpers/disk-reading.ts";/** The refusal mac_disk_policy.read_floor gives when no reading is published. */export const NO_READING = /the device's keep free reading is unavailable/;/** * Publish a fresh reading into `home`, in the shape scripts/dev/machine-row.sh * writes: keepFreeBytes, sampledAt (seconds), volumeId (st_dev of the volume the work is on). * The default floor of one byte admits by the real free space; a test about the floor names its own. */export function publishDiskReading(home: string, { keepFreeBytes = 1, volumeOf = home }: { keepFreeBytes?: number; volumeOf?: string } = {}): string {  const path = join(home, ".cottondb/disk-policy.json");  mkdirSync(join(home, ".cottondb"), { recursive: true });  writeFileSync(path, JSON.stringify({ keepFreeBytes, sampledAt: Date.now() / 1000, volumeId: statSync(volumeOf).dev }));  // The synthetic home is a Mac with the build policy installed: the marker its installer writes last  // (with-build-lock.sh stops on a fleet Mac without it, and builds straight through elsewhere).  mkdirSync(join(home, ".cotton-agent/mac-build-policy"), { recursive: true });  writeFileSync(join(home, ".cotton-agent/mac-build-policy/activated"), "test home\n");  return path;}/** * Record twenty measured build peaks of `bytes` for each build kind in `home`'s * ~/.cottondb/build-peaks.log, the log the policy's RAM admission reads its need * from (mac_disk_policy.memory_need). Without it a fixture's builds each reserve * the class constant (GiBs), so whether two of them may hold the lock together * would turn on how much RAM the machine running the suite has free right now. */export function publishBuildPeaks(home: string, { bytes = 1 }: { bytes?: number } = {}): string {  const path = join(home, ".cottondb/build-peaks.log");  mkdirSync(join(home, ".cottondb"), { recursive: true });  const at = Date.now() / 1000;  const rows = ["swift", "rust"].flatMap((kind) => Array.from({ length: 20 }, () => JSON.stringify({ at, kind, peak: bytes })));  writeFileSync(path, `${rows.join("\n")}\n`);  return path;}let linuxBin = "";/** A PATH directory whose `uname -s` answers Linux and every other `uname` is the real one. */export function linuxHostBin(): string {  if (linuxBin) return linuxBin;  const dir = mkdtempSync(join(tmpdir(), "linux-host-"));  const uname = join(dir, "uname");  writeFileSync(uname, '#!/bin/sh\nif [ "$*" = -s ]; then echo Linux; exit 0; fi\nexec /usr/bin/uname "$@"\n');  chmodSync(uname, 0o755);  linuxBin = dir;  return dir;}/** `PATH` with the Linux `uname` first. */export const linuxHostPath = (path = process.env.PATH ?? "/usr/bin:/bin"): string => `${linuxHostBin()}:${path}`;/** * A failure that is the policy refusing a fixture HOME with no reading, said as the * fixture's mistake: the message gains the helper's name. Undefined for any other failure. */export function forgottenReadingHint(message: string): string | undefined {  if (!NO_READING.test(message) || message.includes(DISK_READING_HELPER)) return undefined;  return `${message}\n\nThis fixture ran a Mac build wrapper under a HOME with no device reading: call ${DISK_READING_HELPER} before it, or run the Linux body with linuxHostPath().`;}