|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | + |
| 7 | +const tempDirs: string[] = []; |
| 8 | +const resolverPath = path.resolve("scripts/secrets/openclaw-bws-resolver.mjs"); |
| 9 | + |
| 10 | +function makeTempDir(): string { |
| 11 | +const dir = mkdtempSync(path.join(tmpdir(), "openclaw-bws-resolver-")); |
| 12 | +tempDirs.push(dir); |
| 13 | +return dir; |
| 14 | +} |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | +for (const dir of tempDirs.splice(0)) { |
| 18 | +rmSync(dir, { force: true, recursive: true }); |
| 19 | +} |
| 20 | +}); |
| 21 | + |
| 22 | +describe("openclaw-bws-resolver", () => { |
| 23 | +it("forwards the self-hosted server URL without inheriting unrelated variables", () => { |
| 24 | +const dir = makeTempDir(); |
| 25 | +const fakeBwsPath = path.join(dir, "bws"); |
| 26 | +writeFileSync( |
| 27 | +fakeBwsPath, |
| 28 | +[ |
| 29 | +"#!/usr/bin/env node", |
| 30 | +'if (process.env.BWS_ACCESS_TOKEN !== "test-token") process.exit(10);', |
| 31 | +'if (process.env.BWS_SERVER_URL !== "https://bws.example.test") process.exit(11);', |
| 32 | +"if (process.env.UNRELATED_PARENT_VALUE !== undefined) process.exit(12);", |
| 33 | +'process.stdout.write(JSON.stringify([{ key: "example", value: "resolved" }]));', |
| 34 | +].join("\n"), |
| 35 | +{ mode: 0o755 }, |
| 36 | +); |
| 37 | +chmodSync(fakeBwsPath, 0o755); |
| 38 | + |
| 39 | +const result = spawnSync(process.execPath, [resolverPath], { |
| 40 | +encoding: "utf8", |
| 41 | +env: { |
| 42 | +BWS_ACCESS_TOKEN: "test-token", |
| 43 | +BWS_BIN: fakeBwsPath, |
| 44 | +BWS_SERVER_URL: "https://bws.example.test", |
| 45 | +PATH: process.env.PATH ?? "", |
| 46 | +UNRELATED_PARENT_VALUE: "do-not-forward", |
| 47 | +}, |
| 48 | +input: JSON.stringify({ protocolVersion: 1, ids: ["example"] }), |
| 49 | +}); |
| 50 | + |
| 51 | +expect(result.status).toBe(0); |
| 52 | +expect(result.stderr).toBe(""); |
| 53 | +expect(JSON.parse(result.stdout)).toEqual({ |
| 54 | +protocolVersion: 1, |
| 55 | +values: { example: "resolved" }, |
| 56 | +errors: {}, |
| 57 | +}); |
| 58 | +}); |
| 59 | +}); |