tinyrelayfile
tinyrelay/scripts/page-link.test.mjs/

scripts/page-link.test.mjs raw
1// The crumb copies the page's public address on a tap and says so briefly.
2import test from "node:test";
3import assert from "node:assert/strict";
4import fs from "node:fs";
5import vm from "node:vm";
6
7const source = fs.readFileSync("tinyclient/components.js", "utf8");
8const linkSource = source.slice(source.indexOf(" // PageLink is the crumb"), source.indexOf(" // JsonView renders"));
9
10function setup({url = "https://relay.example/wiki/notes", clipboard = true} = {}) {
11 const written = [];
12 class HTMLElement {
13 constructor() { this.dataset = {}; this.listeners = {}; }
14 getAttribute(name) { return name === "url" ? url : null; }
15 addEventListener(name, fn) { this.listeners[name] = fn; }
16 }
17 const timers = [];
18 const sandbox = {HTMLElement, navigator: clipboard ? {clipboard: {writeText: async value => { written.push(value); }}} : {}, location: {href: "https://tailnet.example/wiki/notes#x"},
19 setTimeout: (fn, ms) => { timers.push({fn, ms}); return timers.length; }, clearTimeout: () => {}};
20 const Constructor = vm.runInNewContext(`${linkSource}\nPageLink`, sandbox);
21 const link = new Constructor();
22 link.connectedCallback();
23 return {link, written, timers};
24}
25
26test("a tap copies the public address from the server, not the browser's location", async () => {
27 const s = setup();
28 s.link.listeners.click();
29 await new Promise(resolve => setImmediate(resolve));
30 assert.deepEqual(s.written, ["https://relay.example/wiki/notes"]);
31 assert.equal(s.link.dataset.copied, "");
32 assert.equal(s.timers[0].ms, 1500);
33 s.timers[0].fn();
34 assert.equal("copied" in s.link.dataset, false);
35});
36
37test("without a clipboard nothing happens and nothing throws", async () => {
38 const s = setup({clipboard: false});
39 s.link.listeners.click();
40 await new Promise(resolve => setImmediate(resolve));
41 assert.deepEqual(s.written, []);
42 assert.equal("copied" in s.link.dataset, false);
43});

About

ownerc01503e8035f
visibilitypublic
headd730679639bf

Maintainers

  • c01503e8035f owner
  • ba1b5beed5b5 agent hermes

Clone

  • https://012.run/npub1cq2s86qrtadf36eqq3epm38wmhdynrrcwxf84eldk3f94dkv7ttsrulrz5/tinyrelay.git

Refs