import { spawn } from "bun"; import { describe, expect, test } from "bun:test"; import { bunEnv, bunExe } from "harness"; describe("--use-system-ca", () => { test("flag loads system certificates", async () => { // Test that --use-system-ca loads system certificates await using proc = spawn({ cmd: [bunExe(), "--use-system-ca", "-e", "console.log('OK')"], env: bunEnv, stdout: "pipe", stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect(exitCode).toBe(0); expect(stdout.trim()).toBe("OK"); expect(stderr).toBe(""); }); test("NODE_USE_SYSTEM_CA=1 loads system certificates", async () => { // Test that NODE_USE_SYSTEM_CA environment variable works await using proc = spawn({ cmd: [bunExe(), "-e", "console.log('OK')"], env: { ...bunEnv, NODE_USE_SYSTEM_CA: "1" }, stdout: "pipe", stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect(exitCode).toBe(0); expect(stdout.trim()).toBe("OK"); expect(stderr).toBe(""); }); test("NODE_USE_SYSTEM_CA=0 doesn't load system certificates", async () => { // Test that NODE_USE_SYSTEM_CA=0 doesn't load system certificates await using proc = spawn({ cmd: [bunExe(), "-e", "console.log('OK')"], env: { ...bunEnv, NODE_USE_SYSTEM_CA: "0" }, stdout: "pipe", stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect(exitCode).toBe(0); expect(stdout.trim()).toBe("OK"); expect(stderr).toBe(""); }); test("--use-system-ca overrides NODE_USE_SYSTEM_CA=0", async () => { // Test that CLI flag takes precedence over environment variable await using proc = spawn({ cmd: [bunExe(), "--use-system-ca", "-e", "console.log('OK')"], env: { ...bunEnv, NODE_USE_SYSTEM_CA: "0" }, stdout: "pipe", stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect(exitCode).toBe(0); expect(stdout.trim()).toBe("OK"); expect(stderr).toBe(""); }); });