| f0b06dea Andy McCall |
1 |
import { test } from "node:test"; |
| 2 |
import assert from "node:assert/strict"; |
| 3 |
import { makeProvider } from "../out/provider.mjs"; |
| 4 |
import { parseRepoUrl, hubUrl, needsMe } from "../out/hub.mjs"; |
| 5 |
|
| 6 |
test("parses repository URLs on a hub", () => { |
| 7 |
assert.deepEqual(parseRepoUrl("https://earth.example:7343/andy/oxygen"), { base: "https://earth.example:7343", owner: "andy", name: "oxygen" }); |
| 8 |
assert.equal(parseRepoUrl("https://earth.example/api/v1/repos"), undefined); |
| 9 |
assert.equal(parseRepoUrl("file:///tmp/x"), undefined); |
| 10 |
}); |
| 11 |
|
| 12 |
test("builds hub URLs for every target", () => { |
| 13 |
const b = "https://h"; |
| 14 |
assert.equal(hubUrl(b, "a", "r", { kind: "repository" }), "https://h/a/r"); |
| 15 |
assert.equal(hubUrl(b, "a", "r", { kind: "file", reference: "main", path: "src/x.rs", line: 4 }), "https://h/a/r/file/main/src/x.rs#L4"); |
| 16 |
assert.equal(hubUrl(b, "a", "r", { kind: "change", hash: "abc" }), "https://h/a/r/change/abc"); |
| 17 |
assert.equal(hubUrl(b, "a", "r", { kind: "proposal", number: 3 }), "https://h/a/r/proposal/3"); |
| 18 |
}); |
| 19 |
|
| 20 |
test("provider matches only known hubs", () => { |
| 21 |
const p = makeProvider({ bases: () => ["https://h/"] }); |
| 22 |
assert.equal(p.matches("https://h/a/r"), true); |
| 23 |
assert.equal(p.matches("https://other/a/r"), false); |
| 24 |
assert.equal(p.urlFor("https://h/a/r", { kind: "issues" }), "https://h/a/r/issues"); |
| 25 |
}); |
| 26 |
|
| 27 |
test("needsMe: open, not mine, not yet sealed by me", () => { |
| 28 |
const base = { state: "open", author: "bob", approvals: [] }; |
| 29 |
assert.equal(needsMe(base, "andy"), true); |
| 30 |
assert.equal(needsMe({ ...base, author: "andy" }, "andy"), false); |
| 31 |
assert.equal(needsMe({ ...base, approvals: [{ account: "andy", sealed: true, key: "k" }] }, "andy"), false); |
| 32 |
assert.equal(needsMe({ ...base, approvals: [{ account: "andy", sealed: false, key: null }] }, "andy"), true); |
| 33 |
assert.equal(needsMe({ ...base, state: "landed" }, "andy"), false); |
| 34 |
}); |
| 95f69b4a Andy McCall ↶ |
35 |
|
| 36 |
import { hubUrl as hubUrl2 } from "../out/hub.mjs"; |
| 37 |
test("file links carry a line range", () => { |
| 38 |
assert.equal(hubUrl2("https://h", "a", "b", { kind: "file", reference: "main", path: "src/x.rs", line: 3, endLine: 7 }), "https://h/a/b/file/main/src/x.rs#L3-L7"); |
| 39 |
assert.equal(hubUrl2("https://h", "a", "b", { kind: "file", reference: "main", path: "x", line: 3, endLine: 3 }), "https://h/a/b/file/main/x#L3"); |
| 40 |
}); |