| 1 |
import { test } from "node:test"; |
| 2 |
import assert from "node:assert/strict"; |
| 3 |
import { pickProvider, repositoryFor } from "../out/api.mjs"; |
| 4 |
|
| 5 |
test("picks the first provider that matches, preferring origin", () => { |
| 6 |
const hub = { id: "hub", label: "Hub", matches: (u) => u.includes("hub.example"), urlFor: () => undefined }; |
| 7 |
const other = { id: "other", label: "Other", matches: (u) => u.includes("other.example"), urlFor: () => undefined }; |
| 8 |
const remotes = [ |
| 9 |
{ name: "mirror", url: "https://other.example/a/b" }, |
| 10 |
{ name: "origin", url: "https://hub.example/a/b" }, |
| 11 |
]; |
| 12 |
const pick = pickProvider([hub, other], remotes); |
| 13 |
assert.equal(pick.provider.id, "hub"); |
| 14 |
assert.equal(pick.remote.name, "origin"); |
| 15 |
assert.equal(pickProvider([hub], [{ name: "x", url: "https://nowhere" }]), undefined); |
| 16 |
}); |
| 17 |
|
| 18 |
test("repositoryFor picks the longest matching root", () => { |
| 19 |
const roots = ["/w/a", "/w/a/sub", "/w/b"]; |
| 20 |
assert.equal(repositoryFor(roots, "/w/a/sub/file.rs"), "/w/a/sub"); |
| 21 |
assert.equal(repositoryFor(roots, "/w/a/x.rs"), "/w/a"); |
| 22 |
assert.equal(repositoryFor(roots, "/w/ab/x"), undefined); |
| 23 |
}); |
| 24 |
|
| 25 |
test("permalink state addresses match OxygenHub's", async () => { |
| 26 |
const { stateSpec } = await import("../out/api.mjs"); |
| 27 |
const a = "b".repeat(64), b = "a".repeat(64); |
| 28 |
assert.equal(stateSpec([a, b, a.toUpperCase()]), undefined, "mixed-case duplicates are not a state"); |
| 29 |
assert.equal(stateSpec([a, b]), `${b}+${a}`, "sorted and joined with +"); |
| 30 |
assert.equal(stateSpec([a]), a); |
| 31 |
assert.equal(stateSpec([]), undefined); |
| 32 |
assert.equal(stateSpec(["abc"]), undefined, "full hashes only"); |
| 33 |
}); |
| 34 |
|
| 35 |
test("linked lines follow the click and the selection", async () => { |
| 36 |
const { linkedLines } = await import("../out/api.mjs"); |
| 37 |
const none = { start: 5, end: 5, endColumn: 3, empty: true }; |
| 38 |
assert.deepEqual(linkedLines(undefined, none), { line: 5 }); |
| 39 |
assert.deepEqual(linkedLines(12, none), { line: 12 }, "gutter click without a selection"); |
| 40 |
const sel = { start: 10, end: 20, endColumn: 4, empty: false }; |
| 41 |
assert.deepEqual(linkedLines(undefined, sel), { line: 10, endLine: 20 }); |
| 42 |
assert.deepEqual(linkedLines(15, sel), { line: 10, endLine: 20 }, "a click inside the selection keeps it"); |
| 43 |
assert.deepEqual(linkedLines(30, sel), { line: 30 }, "a click outside wins"); |
| 44 |
assert.deepEqual(linkedLines(undefined, { start: 10, end: 21, endColumn: 0, empty: false }), { line: 10, endLine: 20 }, "a selection ending at column 0 stops the line before"); |
| 45 |
assert.deepEqual(linkedLines(undefined, { start: 7, end: 7, endColumn: 9, empty: false }), { line: 7 }); |
| 46 |
}); |