Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(scripts): cover saas ambient script behaviour

Steven Enamakel2dc0c46be38f
diff --git a/scripts/__tests__/saas-ambient.test.mjs b/scripts/__tests__/saas-ambient.test.mjs
new file mode 100644
index 0000000000..0ba96bd49e
--- /dev/null
+++ b/scripts/__tests__/saas-ambient.test.mjs
@@ -0,0 +1,110 @@
+// Unit tests for scripts/ci/check-saas-ambient.mjs — the ratchet over code
+// that reaches past a SaaS user agent's CoreContext.
+
+import assert from "node:assert/strict";
+import { spawnSync } from "node:child_process";
+import fs from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import test from "node:test";
+import { fileURLToPath } from "node:url";
+
+import { codeOf, compare, scan } from "../ci/check-saas-ambient.mjs";
+
+const repoRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "..");
+const script = path.join(repoRoot, "scripts", "ci", "check-saas-ambient.mjs");
+const SRC = "crates/openhuman-core/src";
+
+function run(files, baseline, args = []) {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "openhuman-saas-ambient-"));
+ for (const [rel, body] of Object.entries(files)) {
+ const abs = path.join(root, rel);
+ fs.mkdirSync(path.dirname(abs), { recursive: true });
+ fs.writeFileSync(abs, body);
+ }
+ if (baseline) {
+ const abs = path.join(root, "scripts", "ci", "saas-ambient-baseline.json");
+ fs.mkdirSync(path.dirname(abs), { recursive: true });
+ fs.writeFileSync(abs, `${JSON.stringify(baseline, null, 2)}\n`);
+ }
+ const result = spawnSync(process.execPath, [script, "--root", root, ...args], {
+ encoding: "utf8",
+ });
+ const written = path.join(root, "scripts", "ci", "saas-ambient-baseline.json");
+ const after = fs.existsSync(written) ? JSON.parse(fs.readFileSync(written, "utf8")) : null;
+ fs.rmSync(root, { recursive: true, force: true });
+ return { status: result.status, out: `${result.stdout}${result.stderr}`, after };
+}
+
+test("flags every rule", () => {
+ const found = scan(
+ "x.rs",
+ [
+ "tokio::spawn(async {});",
+ "let h = tokio::task::spawn_blocking(|| 1);",
+ "std::thread::spawn(|| {});",
+ "let c = Config::load_or_init().await?;",
+ "std::env::set_var(\"A\", \"b\");",
+ "let h = dirs::home_dir();",
+ ].join("\n"),
+ );
+ assert.deepEqual(
+ found.map((f) => f.rule),
+ ["bare-spawn", "bare-spawn", "bare-spawn", "ambient-config", "env-mutation", "home-dir"],
+ );
+});
+
+test("ignores comments, docs and the scoped helpers", () => {
+ const found = scan(
+ "x.rs",
+ [
+ "// tokio::spawn(fut) drops the scope",
+ "/// call Config::load_or_init() only in single-user code",
+ "spawn_scoped(async {});",
+ "let s = \"// not a comment\"; tokio::spawn(f);",
+ ].join("\n"),
+ );
+ assert.deepEqual(found.map((f) => f.rule), ["bare-spawn"]);
+ assert.equal(codeOf("a(); // tokio::spawn("), "a(); ");
+});
+
+test("counts repeated identical lines as separate occurrences", () => {
+ const found = scan("x.rs", "tokio::spawn(f);\ntokio::spawn(f);\n");
+ assert.deepEqual(found.map((f) => f.occurrence), [1, 2]);
+});
+
+test("a moved line is not a new site, a new line is", () => {
+ const before = scan("x.rs", "tokio::spawn(f);\n");
+ const moved = scan("x.rs", "\n\ntokio::spawn(f);\n");
+ assert.deepEqual(compare(moved, before), { added: [], stale: [] });
+ const grown = scan("x.rs", "tokio::spawn(f);\ntokio::spawn(g);\n");
+ assert.equal(compare(grown, before).added.length, 1);
+});
+
+test("passes on a matching baseline and skips test files", () => {
+ const files = {
+ [`${SRC}/a.rs`]: "tokio::spawn(f);\n",
+ [`${SRC}/a_tests.rs`]: "tokio::spawn(f);\n",
+ [`${SRC}/core/runtime/spawn.rs`]: "tokio::spawn(f);\n",
+ };
+ const first = run(files, null, ["--write-baseline"]);
+ assert.equal(first.status, 0, first.out);
+ assert.equal(first.after.length, 1, "only the non-test, non-helper site");
+ const again = run(files, first.after);
+ assert.equal(again.status, 0, again.out);
+ assert.match(again.out, /ratchet holds \(1 /);
+});
+
+test("fails on a new site and names the fix", () => {
+ const { status, out } = run({ [`${SRC}/a.rs`]: "Config::load_or_init();\n" }, []);
+ assert.equal(status, 1);
+ assert.match(out, /ambient-config: crates\/openhuman-core\/src\/a.rs:1/);
+ assert.match(out, /load_config_with_timeout/);
+});
+
+test("fails on a fixed site still in the baseline", () => {
+ const baseline = scan(`${SRC}/a.rs`, "tokio::spawn(f);\n");
+ const { status, out } = run({ [`${SRC}/a.rs`]: "spawn_scoped(f);\n" }, baseline);
+ assert.equal(status, 1);
+ assert.match(out, /remove them/);
+});