|
| 1 | +import { describe, it } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { buildRestrictExample } from "./build-example.mjs"; |
| 4 | + |
| 5 | +const REPO = "dash14/buildcage"; |
| 6 | + |
| 7 | +function wrap(yaml) { |
| 8 | + return ( |
| 9 | + "\n<details>\n" + |
| 10 | + "<summary>🔒 Switch to restrict mode</summary>\n\n" + |
| 11 | + "```yaml\n" + |
| 12 | + yaml + |
| 13 | + "```\n\n" + |
| 14 | + "</details>\n" |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +describe("buildRestrictExample", () => { |
| 19 | + it("empty array → empty string", () => { |
| 20 | + assert.equal(buildRestrictExample([], REPO), ""); |
| 21 | + }); |
| 22 | + |
| 23 | + it("null/undefined → empty string", () => { |
| 24 | + assert.equal(buildRestrictExample(null, REPO), ""); |
| 25 | + assert.equal(buildRestrictExample(undefined, REPO), ""); |
| 26 | + }); |
| 27 | + |
| 28 | + it("HTTPS only entries", () => { |
| 29 | + const rows = [ |
| 30 | + { host: "registry.npmjs.org", port: "443", ruleType: "HTTPS", count: 5 }, |
| 31 | + { host: "github.com", port: "443", ruleType: "HTTPS", count: 2 }, |
| 32 | + ]; |
| 33 | + assert.equal( |
| 34 | + buildRestrictExample(rows, REPO), |
| 35 | + wrap( |
| 36 | + [ |
| 37 | + "- name: Start Buildcage in restrict mode", |
| 38 | + ` uses: ${REPO}/setup@v1`, |
| 39 | + " with:", |
| 40 | + " proxy_mode: restrict", |
| 41 | + " allowed_https_rules: >-", |
| 42 | + " registry.npmjs.org:443", |
| 43 | + " github.com:443", |
| 44 | + ].join("\n") + "\n", |
| 45 | + ) |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it("HTTP + HTTPS mixed entries", () => { |
| 50 | + const rows = [ |
| 51 | + { host: "registry.npmjs.org", port: "443", ruleType: "HTTPS", count: 3 }, |
| 52 | + { host: "deb.debian.org", port: "80", ruleType: "HTTP", count: 1 }, |
| 53 | + ]; |
| 54 | + assert.equal( |
| 55 | + buildRestrictExample(rows, REPO), |
| 56 | + wrap( |
| 57 | + [ |
| 58 | + "- name: Start Buildcage in restrict mode", |
| 59 | + ` uses: ${REPO}/setup@v1`, |
| 60 | + " with:", |
| 61 | + " proxy_mode: restrict", |
| 62 | + " allowed_https_rules: >-", |
| 63 | + " registry.npmjs.org:443", |
| 64 | + " allowed_http_rules: >-", |
| 65 | + " deb.debian.org:80", |
| 66 | + ].join("\n") + "\n", |
| 67 | + ) |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it("IP entries", () => { |
| 72 | + const rows = [ |
| 73 | + { host: "192.168.1.1", port: "443", ruleType: "IP", count: 1 }, |
| 74 | + ]; |
| 75 | + assert.equal( |
| 76 | + buildRestrictExample(rows, REPO), |
| 77 | + wrap( |
| 78 | + [ |
| 79 | + "- name: Start Buildcage in restrict mode", |
| 80 | + ` uses: ${REPO}/setup@v1`, |
| 81 | + " with:", |
| 82 | + " proxy_mode: restrict", |
| 83 | + " allowed_ip_rules: >-", |
| 84 | + " 192.168.1.1:443", |
| 85 | + ].join("\n") + "\n", |
| 86 | + ) |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it("all three rule types", () => { |
| 91 | + const rows = [ |
| 92 | + { host: "example.com", port: "443", ruleType: "HTTPS", count: 2 }, |
| 93 | + { host: "example.com", port: "80", ruleType: "HTTP", count: 1 }, |
| 94 | + { host: "10.0.0.1", port: "8080", ruleType: "IP", count: 1 }, |
| 95 | + ]; |
| 96 | + assert.equal( |
| 97 | + buildRestrictExample(rows, REPO), |
| 98 | + wrap( |
| 99 | + [ |
| 100 | + "- name: Start Buildcage in restrict mode", |
| 101 | + ` uses: ${REPO}/setup@v1`, |
| 102 | + " with:", |
| 103 | + " proxy_mode: restrict", |
| 104 | + " allowed_https_rules: >-", |
| 105 | + " example.com:443", |
| 106 | + " allowed_http_rules: >-", |
| 107 | + " example.com:80", |
| 108 | + " allowed_ip_rules: >-", |
| 109 | + " 10.0.0.1:8080", |
| 110 | + ].join("\n") + "\n", |
| 111 | + ) |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it("uses custom actionRepo", () => { |
| 116 | + const rows = [ |
| 117 | + { host: "example.com", port: "443", ruleType: "HTTPS", count: 1 }, |
| 118 | + ]; |
| 119 | + assert.equal( |
| 120 | + buildRestrictExample(rows, "myorg/myrepo"), |
| 121 | + wrap( |
| 122 | + [ |
| 123 | + "- name: Start Buildcage in restrict mode", |
| 124 | + " uses: myorg/myrepo/setup@v1", |
| 125 | + " with:", |
| 126 | + " proxy_mode: restrict", |
| 127 | + " allowed_https_rules: >-", |
| 128 | + " example.com:443", |
| 129 | + ].join("\n") + "\n", |
| 130 | + ) |
| 131 | + ); |
| 132 | + }); |
| 133 | +}); |
0 commit comments