anvilsign in

collin/browser-terminal-extension

main / extension / lib / portless.test.js
1// Run with: node --test "extension/lib/*.test.js"
2//
3// The whole value of this module is agreeing with portless about what a project
4// is called, so these are mostly the naming rules read back: the sanitizing it
5// does to make a DNS label, and the worktree prefix it puts in front.
6
7const test = require("node:test");
8const assert = require("node:assert");
9
10const Portless = require("./portless.js");
11
12test("sanitize spells a name the way a hostname must", () => {
13 assert.equal(Portless.sanitize("MyApp"), "myapp");
14 // Scopes are stripped before this by portless; everything else that is not a
15 // hostname character collapses to a single dash, and dashes never bookend.
16 assert.equal(Portless.sanitize("My App_v2"), "my-app-v2");
17 assert.equal(Portless.sanitize("--edge--case--"), "edge-case");
18 assert.equal(Portless.sanitize("café"), "caf");
19 assert.equal(Portless.sanitize(""), "");
20 // Past a DNS label portless appends a hash we cannot reproduce, so nothing
21 // matches rather than something wrong matching.
22 assert.equal(Portless.sanitize("a".repeat(63)).length, 63);
23 assert.equal(Portless.sanitize("a".repeat(64)), "");
24});
25
26test("split reads the project and any worktree label off a host", () => {
27 assert.deepEqual(Portless.split("mdlab.localhost"), { name: "mdlab", prefix: "" });
28 assert.deepEqual(Portless.split("feat-x.mdlab.localhost"), { name: "mdlab", prefix: "feat-x" });
29 assert.deepEqual(Portless.split("MDLAB.LOCALHOST"), { name: "mdlab", prefix: "" });
30});
31
32test("split is what keeps detection local", () => {
33 // Not portless's suffix, so not portless's business — `.localhost` is
34 // reserved for this machine and a public name cannot end in it.
35 for (const host of ["mdlab.local", "example.com", "localhost", "127.0.0.1", ""]) {
36 assert.equal(Portless.split(host), null, host);
37 }
38});
39
40test("owns matches the project a plain host names", () => {
41 assert.ok(Portless.owns("mdlab.localhost", "mdlab"));
42 // The name is sanitized on the way in, so a directory called `My Lab` is the
43 // host `my-lab.localhost`.
44 assert.ok(Portless.owns("my-lab.localhost", "My Lab"));
45 assert.ok(!Portless.owns("mdlab.localhost", "dotfiles"));
46 assert.ok(!Portless.owns("mdlab.localhost", ""));
47});
48
49test("a worktree host wants the worktree, not the checkout it forked from", () => {
50 // The prefix is a branch and a session is a directory, so what matches is a
51 // directory named after the branch, alone or beside the project.
52 assert.ok(Portless.owns("feat-x.mdlab.localhost", "feat-x"));
53 assert.ok(Portless.owns("feat-x.mdlab.localhost", "mdlab-feat-x"));
54 assert.ok(Portless.owns("feat-x.mdlab.localhost", "feat-x-mdlab"));
55 // The main checkout answers to `mdlab.localhost` and only that one.
56 assert.ok(!Portless.owns("feat-x.mdlab.localhost", "mdlab"));
57 assert.ok(!Portless.owns("mdlab.localhost", "feat-x"));
58});