|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { humanizeConfigKey, summarizeRunOn } from "./runOnSummary.ts"; |
| 5 | + |
| 6 | +test("local backend summarizes to the local location with no rows", () => { |
| 7 | + assert.deepEqual(summarizeRunOn({ type: "local" }), { location: "local" }); |
| 8 | +}); |
| 9 | + |
| 10 | +test("provider backend carries the provider id", () => { |
| 11 | + const summary = summarizeRunOn({ |
| 12 | + type: "provider", |
| 13 | + id: "kubernetes", |
| 14 | + config: {}, |
| 15 | + }); |
| 16 | + assert.equal(summary.location, "provider"); |
| 17 | + assert.equal(summary.providerId, "kubernetes"); |
| 18 | + assert.deepEqual(summary.rows, []); |
| 19 | +}); |
| 20 | + |
| 21 | +test("a saved kubernetes config renders labeled scalar rows", () => { |
| 22 | + const summary = summarizeRunOn({ |
| 23 | + type: "provider", |
| 24 | + id: "kubernetes", |
| 25 | + config: { |
| 26 | + namespace: "buzz-agents-x7k2mp", |
| 27 | + image: "ghcr.io/block/buzz-sprig@sha256:17facfc7", |
| 28 | + cpu_request: "1", |
| 29 | + inactivity_seconds: 7200, |
| 30 | + }, |
| 31 | + }); |
| 32 | + assert.equal(summary.location, "provider"); |
| 33 | + const byKey = Object.fromEntries(summary.rows.map((r) => [r.key, r])); |
| 34 | + assert.equal(byKey.namespace.label, "Namespace"); |
| 35 | + assert.equal(byKey.namespace.value, "buzz-agents-x7k2mp"); |
| 36 | + assert.equal(byKey.cpu_request.label, "CPU request"); |
| 37 | + assert.equal(byKey.cpu_request.value, "1"); |
| 38 | + assert.equal(byKey.inactivity_seconds.label, "Inactivity seconds"); |
| 39 | + assert.equal(byKey.inactivity_seconds.value, "7200"); |
| 40 | + assert.equal(byKey.image.value, "ghcr.io/block/buzz-sprig@sha256:17facfc7"); |
| 41 | + assert.ok(summary.rows.every((r) => r.redacted === false)); |
| 42 | +}); |
| 43 | + |
| 44 | +test("rows follow the provider-schema preferred order, spillover alphabetical", () => { |
| 45 | + const summary = summarizeRunOn({ |
| 46 | + type: "provider", |
| 47 | + id: "kubernetes", |
| 48 | + config: { |
| 49 | + // Deliberately shuffled persisted order. |
| 50 | + memory_limit: "1Gi", |
| 51 | + zeta_extra: "z", |
| 52 | + namespace: "n", |
| 53 | + cpu_limit: "1", |
| 54 | + alpha_extra: "a", |
| 55 | + image: "i", |
| 56 | + inactivity_seconds: 7200, |
| 57 | + cpu_request: "1", |
| 58 | + context: "c", |
| 59 | + memory_request: "1Gi", |
| 60 | + }, |
| 61 | + }); |
| 62 | + assert.deepEqual( |
| 63 | + summary.rows.map((r) => r.key), |
| 64 | + [ |
| 65 | + "context", |
| 66 | + "namespace", |
| 67 | + "image", |
| 68 | + "cpu_request", |
| 69 | + "memory_request", |
| 70 | + "cpu_limit", |
| 71 | + "memory_limit", |
| 72 | + "inactivity_seconds", |
| 73 | + "alpha_extra", |
| 74 | + "zeta_extra", |
| 75 | + ], |
| 76 | + ); |
| 77 | +}); |
| 78 | + |
| 79 | +test("null, empty-string, boolean, and number values all display honestly", () => { |
| 80 | + const summary = summarizeRunOn({ |
| 81 | + type: "provider", |
| 82 | + id: "p", |
| 83 | + config: { a_null: null, b_empty: "", c_flag: true, d_num: 0, e_off: false }, |
| 84 | + }); |
| 85 | + const values = Object.fromEntries(summary.rows.map((r) => [r.key, r.value])); |
| 86 | + assert.equal(values.a_null, "Not set"); |
| 87 | + assert.equal(values.b_empty, "Not set"); |
| 88 | + assert.equal(values.c_flag, "true"); |
| 89 | + // Falsy-but-present values must never read as missing: coerceConfigValues |
| 90 | + // emits real numbers/booleans, so `value || fallback` would lie about 0/false. |
| 91 | + assert.equal(values.d_num, "0"); |
| 92 | + assert.equal(values.e_off, "false"); |
| 93 | +}); |
| 94 | + |
| 95 | +test("every row value is a string — objects must never reach React children", () => { |
| 96 | + // React throws on an object child, taking down the whole edit dialog. |
| 97 | + // A hand-edited managed-agents.json with nested config is exactly the |
| 98 | + // record the create-time scalar gate never saw. |
| 99 | + const summary = summarizeRunOn({ |
| 100 | + type: "provider", |
| 101 | + id: "p", |
| 102 | + config: { |
| 103 | + resources: { limits: { cpu: "2" } }, |
| 104 | + flag: true, |
| 105 | + count: 3, |
| 106 | + name: "x", |
| 107 | + missing: null, |
| 108 | + }, |
| 109 | + }); |
| 110 | + for (const row of summary.rows) { |
| 111 | + assert.equal(typeof row.value, "string", `${row.key} is not a string`); |
| 112 | + } |
| 113 | +}); |
| 114 | + |
| 115 | +test("arrays and objects are summarized, never serialized", () => { |
| 116 | + const summary = summarizeRunOn({ |
| 117 | + type: "provider", |
| 118 | + id: "p", |
| 119 | + config: { |
| 120 | + tolerations: [{ key: "gpu" }, { key: "spot" }], |
| 121 | + node_selector: { disktype: "ssd" }, |
| 122 | + }, |
| 123 | + }); |
| 124 | + const values = Object.fromEntries(summary.rows.map((r) => [r.key, r.value])); |
| 125 | + assert.equal(values.tolerations, "List (2 items)"); |
| 126 | + assert.equal(values.node_selector, "Structured value"); |
| 127 | + // The nested content must not leak through. |
| 128 | + assert.ok(!JSON.stringify(values).includes("ssd")); |
| 129 | +}); |
| 130 | + |
| 131 | +test("secret-shaped keys are redacted, fail-safe for unknown providers", () => { |
| 132 | + const summary = summarizeRunOn({ |
| 133 | + type: "provider", |
| 134 | + id: "future-provider", |
| 135 | + config: { |
| 136 | + api_token: "abc123", |
| 137 | + registry_password: "hunter2", |
| 138 | + clientSecret: "s3cr3t", |
| 139 | + authHeader: "Bearer xyz", |
| 140 | + privateKey: "nsec1...", |
| 141 | + namespace: "safe-to-show", |
| 142 | + }, |
| 143 | + }); |
| 144 | + const byKey = Object.fromEntries(summary.rows.map((r) => [r.key, r])); |
| 145 | + for (const key of [ |
| 146 | + "api_token", |
| 147 | + "registry_password", |
| 148 | + "clientSecret", |
| 149 | + "authHeader", |
| 150 | + "privateKey", |
| 151 | + ]) { |
| 152 | + assert.equal(byKey[key].redacted, true, `${key} must be redacted`); |
| 153 | + assert.equal(byKey[key].value, "••••••••"); |
| 154 | + } |
| 155 | + assert.equal(byKey.namespace.redacted, false); |
| 156 | + assert.equal(byKey.namespace.value, "safe-to-show"); |
| 157 | + // Raw secret bytes must be absent from the whole summary. |
| 158 | + const dump = JSON.stringify(summary); |
| 159 | + for (const secret of ["abc123", "hunter2", "s3cr3t", "Bearer xyz", "nsec1"]) { |
| 160 | + assert.ok(!dump.includes(secret), `${secret} leaked`); |
| 161 | + } |
| 162 | +}); |
| 163 | + |
| 164 | +test("humanizeConfigKey handles snake_case, camelCase, and acronyms", () => { |
| 165 | + assert.equal(humanizeConfigKey("cpu_request"), "CPU request"); |
| 166 | + assert.equal(humanizeConfigKey("memory_limit"), "Memory limit"); |
| 167 | + assert.equal(humanizeConfigKey("serviceAccount"), "Service account"); |
| 168 | + assert.equal(humanizeConfigKey("image"), "Image"); |
| 169 | + assert.equal(humanizeConfigKey("api_url"), "API URL"); |
| 170 | + assert.equal(humanizeConfigKey(""), ""); |
| 171 | +}); |
0 commit comments