|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | 4 | import assert from "node:assert/strict"; |
5 | | -import { describe, it, expect } from "vitest"; |
| 5 | +import { describe, it, expect, vi } from "vitest"; |
6 | 6 | import path from "node:path"; |
7 | 7 | import policies from "../bin/lib/policies"; |
8 | 8 |
|
@@ -67,6 +67,45 @@ describe("policies", () => { |
67 | 67 | expect(hosts.length > 0).toBeTruthy(); |
68 | 68 | } |
69 | 69 | }); |
| 70 | + |
| 71 | + it("strips surrounding quotes from hostnames", () => { |
| 72 | + const yaml = 'host: "example.com"\n host: \'other.com\''; |
| 73 | + const hosts = policies.getPresetEndpoints(yaml); |
| 74 | + expect(hosts).toEqual(["example.com", "other.com"]); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe("applyPreset disclosure logging", () => { |
| 79 | + it("logs egress endpoints before applying", () => { |
| 80 | + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 81 | + const errSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 82 | + const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => { throw new Error("exit"); }); |
| 83 | + |
| 84 | + try { |
| 85 | + policies.applyPreset("test-sandbox", "npm"); |
| 86 | + } catch {} |
| 87 | + |
| 88 | + const messages = logSpy.mock.calls.map((c) => c[0]); |
| 89 | + expect(messages.some((m) => typeof m === "string" && m.includes("Widening sandbox egress"))).toBe(true); |
| 90 | + |
| 91 | + logSpy.mockRestore(); |
| 92 | + errSpy.mockRestore(); |
| 93 | + exitSpy.mockRestore(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("does not log when preset has no endpoints", () => { |
| 97 | + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 98 | + const errSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 99 | + |
| 100 | + // loadPreset returns null for nonexistent presets → early return |
| 101 | + policies.applyPreset("test-sandbox", "nonexistent"); |
| 102 | + |
| 103 | + const messages = logSpy.mock.calls.map((c) => c[0]); |
| 104 | + expect(messages.some((m) => typeof m === "string" && m.includes("Widening sandbox egress"))).toBe(false); |
| 105 | + |
| 106 | + logSpy.mockRestore(); |
| 107 | + errSpy.mockRestore(); |
| 108 | + }); |
70 | 109 | }); |
71 | 110 |
|
72 | 111 | describe("buildPolicySetCommand", () => { |
|
0 commit comments