cloud / internal-packages / installer / test / run-log.mjsmain at aacbc74
// The lines of a run's log that its state keeps (worker/run-log.js), and what the page then reads
// from them (src/deploy-steps.ts). Run: node test/run-log.mjs
import assert from "node:assert/strict";
import { test } from "node:test";
import { keptLines } from "../worker/run-log.js";
const many = (n, word) => Array.from({ length: n }, (_, i) => `${word} ${i + 1}`);
const stepsIn = (lines) => lines.filter((l) => l.startsWith("== step ")).map((l) => l.slice(8));
test("a short log is kept whole", () => {
const lines = ["== step check", "account: an account", "== step deploy", ...many(397, "deploy")];
assert.equal(lines.length, 400);
assert.equal(keptLines(lines), lines);
});
test("a long deploy: every step that the run reached is still in the lines, in order", () => {
const lines = [
"before the first step",
"== step check",
...many(5, "check"),
"== step erase",
"Erased: Worker iterate-example-com-auth",
"Left: R2 bucket iterate-example-com-telemetry. No file is in it.",
"Note: The R2 bucket iterate-example-com-telemetry is left in your Cloudflare account.",
"== step deploy",
...many(1300, "deploy"),
];
const kept = keptLines(lines);
assert.deepEqual(stepsIn(kept), ["check", "erase", "deploy"]);
// the end of the log is whole
assert.deepEqual(kept.slice(-400), lines.slice(-400));
// the erase step is whole: what was erased, what was left, and its note
const erase = kept.slice(kept.indexOf("== step erase") + 1, kept.indexOf("== step deploy"));
assert.deepEqual(
erase,
lines.slice(lines.indexOf("== step erase") + 1, lines.indexOf("== step deploy")),
);
// each part that is cut says its size, in its own step
assert.equal(kept[0], "(1 line of the log not kept here)");
assert.equal(kept[kept.indexOf("== step check") + 1], "(5 lines of the log not kept here)");
assert.equal(kept[kept.indexOf("== step deploy") + 1], "(900 lines of the log not kept here)");
assert.equal(kept.length, 1 + 2 + 1 + 3 + 2 + 400);
});
test("a note from the part that is cut is kept, in its step", () => {
const note = "Note: this update set mind.sandboxImage to 'published'.";
const lines = ["== step check", "== step deploy", "deploy 0", note, ...many(600, "deploy")];
const kept = keptLines(lines);
assert.deepEqual(kept.slice(0, 4), [
"== step check",
"== step deploy",
note,
"(201 lines of the log not kept here)",
]);
assert.equal(kept.length, 404);
});
test("an erase that says very much: its first 300 lines are kept, and the rest is counted", () => {
const lines = [
"== step erase",
...many(450, "Erased: record"),
"== step deploy",
...many(500, "deploy"),
];
const kept = keptLines(lines);
assert.equal(kept[300], "Erased: record 300");
assert.equal(kept[301], "(150 lines of the log not kept here)");
assert.deepEqual(kept.slice(302, 304), [
"== step deploy",
"(100 lines of the log not kept here)",
]);
assert.equal(kept.length, 1 + 300 + 1 + 2 + 400);
});
test("a step that starts in the end of the log is there once", () => {
const lines = ["== step check", ...many(450, "check"), "== step deploy", ...many(10, "deploy")];
const kept = keptLines(lines);
assert.deepEqual(stepsIn(kept), ["check", "deploy"]);
assert.equal(kept[1], "(61 lines of the log not kept here)");
});