|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { test } from "node:test"; |
| 3 | + |
| 4 | +import { deriveRelayCloneUrl, effectiveCloneUrls } from "./projectCloneUrl.ts"; |
| 5 | + |
| 6 | +const OWNER = "a".repeat(64); |
| 7 | +const ORIGIN = "https://relay.example"; |
| 8 | + |
| 9 | +test("deriveRelayCloneUrl builds the canonical relay-hosted path", () => { |
| 10 | + assert.equal( |
| 11 | + deriveRelayCloneUrl(ORIGIN, OWNER, "flappy-bee"), |
| 12 | + `${ORIGIN}/git/${OWNER}/flappy-bee`, |
| 13 | + ); |
| 14 | +}); |
| 15 | + |
| 16 | +test("deriveRelayCloneUrl lowercases the owner pubkey", () => { |
| 17 | + const upper = "A".repeat(64); |
| 18 | + assert.equal( |
| 19 | + deriveRelayCloneUrl(ORIGIN, upper, "repo"), |
| 20 | + `${ORIGIN}/git/${OWNER}/repo`, |
| 21 | + ); |
| 22 | +}); |
| 23 | + |
| 24 | +test("deriveRelayCloneUrl tolerates a trailing slash on the origin", () => { |
| 25 | + assert.equal( |
| 26 | + deriveRelayCloneUrl(`${ORIGIN}/`, OWNER, "repo"), |
| 27 | + `${ORIGIN}/git/${OWNER}/repo`, |
| 28 | + ); |
| 29 | +}); |
| 30 | + |
| 31 | +test("deriveRelayCloneUrl fails closed on an unresolved origin", () => { |
| 32 | + assert.equal(deriveRelayCloneUrl(null, OWNER, "repo"), null); |
| 33 | + assert.equal(deriveRelayCloneUrl(undefined, OWNER, "repo"), null); |
| 34 | + assert.equal(deriveRelayCloneUrl("", OWNER, "repo"), null); |
| 35 | +}); |
| 36 | + |
| 37 | +test("deriveRelayCloneUrl declines a non-hex or wrong-length owner", () => { |
| 38 | + assert.equal(deriveRelayCloneUrl(ORIGIN, "short", "repo"), null); |
| 39 | + assert.equal(deriveRelayCloneUrl(ORIGIN, "z".repeat(64), "repo"), null); |
| 40 | +}); |
| 41 | + |
| 42 | +test("deriveRelayCloneUrl declines a missing repo id", () => { |
| 43 | + assert.equal(deriveRelayCloneUrl(ORIGIN, OWNER, ""), null); |
| 44 | +}); |
| 45 | + |
| 46 | +test("effectiveCloneUrls honors explicit clone URLs over the derived default", () => { |
| 47 | + const explicit = ["https://github.com/octocat/hello"]; |
| 48 | + assert.deepEqual( |
| 49 | + effectiveCloneUrls(explicit, ORIGIN, OWNER, "repo"), |
| 50 | + explicit, |
| 51 | + ); |
| 52 | +}); |
| 53 | + |
| 54 | +test("effectiveCloneUrls derives a default when none is advertised", () => { |
| 55 | + assert.deepEqual(effectiveCloneUrls([], ORIGIN, OWNER, "flappy-bee"), [ |
| 56 | + `${ORIGIN}/git/${OWNER}/flappy-bee`, |
| 57 | + ]); |
| 58 | +}); |
| 59 | + |
| 60 | +test("effectiveCloneUrls returns empty when no default can be derived", () => { |
| 61 | + assert.deepEqual(effectiveCloneUrls([], null, OWNER, "repo"), []); |
| 62 | +}); |
0 commit comments