OXYGENHUB
◆ v0.13.0 oxygenhub-vscode / test / dropkit.test.mjs
3.4 KiB · last changed OxygenHub 0.13.0: Drops in the editor with Share as a Drop (closes #7); Flow approvals, expedite, reports, timings and test results (closes #8); ready/draft, landable, trust reports, asset downloads and new repositories (closes #9) 6 days ago source blame history raw
991516a1 Andy McCall ↶ 1
import { test } from "node:test";
2
import assert from "node:assert/strict";
3
import { dedent, dropBaseOf, dropIdOf, dropLabel, dropUriParts, embedSnippet, parseDropUri, shareNames, snippetName, snippetTitle, commentLine } from "../out/dropkit.mjs";
4
5
const ID = "0123456789abcdef01234567";
6
7
test("drop file addresses round trip, folders and all", () => {
8
  const r = { base: "https://hub.example:7343", id: ID, name: "src/lib/a.ts" };
9
  const { path, query } = dropUriParts(r);
10
  assert.equal(path, `/${ID}/src/lib/a.ts`);
11
  assert.deepEqual(parseDropUri(path, query), r);
12
  assert.deepEqual(parseDropUri(`/${ID}`, query), { ...r, name: "" }, "the drop itself");
13
  assert.equal(parseDropUri("/not-an-id/a.ts", query), undefined);
14
  assert.equal(parseDropUri(path, ""), undefined, "no hub, no drop");
15
});
16
17
test("a pasted drop is its id or its page", () => {
18
  assert.equal(dropIdOf(ID), ID);
19
  assert.equal(dropIdOf(` https://oxygenhub.io/andy/drops/${ID} `), ID);
20
  assert.equal(dropIdOf(`https://oxygenhub.io/drops/${ID}?tab=history`), ID);
21
  assert.equal(dropIdOf("https://oxygenhub.io/andy/oxygen"), undefined);
22
  assert.equal(dropBaseOf(`https://oxygenhub.io/andy/drops/${ID}`), "https://oxygenhub.io");
23
  assert.equal(dropBaseOf(ID), undefined);
24
});
25
26
test("shared files keep their paths below the folder they share", () => {
27
  assert.deepEqual(shareNames(["/w/p/src/a/index.ts"]), ["index.ts"], "one file: its name");
28
  assert.deepEqual(shareNames(["/w/p/src/a/index.ts", "/w/p/src/b/index.ts"]), ["a/index.ts", "b/index.ts"], "two index.ts stay apart");
29
  assert.deepEqual(shareNames(["/w/p/README.md", "/w/p/src/main.rs"]), ["README.md", "src/main.rs"]);
30
  assert.deepEqual(shareNames(["C:\\w\\p\\a.txt", "C:\\w\\p\\b.txt"]), ["a.txt", "b.txt"], "Windows paths");
31
});
32
33
test("a selection is named and titled by its file and lines", () => {
34
  assert.equal(snippetName("/w/p/src/main.rs", "rust"), "main.rs");
35
  assert.equal(snippetName(undefined, "python"), "snippet.py", "an untitled editor by its language");
36
  assert.equal(snippetName(undefined, "cobol"), "snippet.txt");
37
  assert.equal(snippetTitle("main.rs", 3, 9, false), "main.rs, lines 3–9");
38
  assert.equal(snippetTitle("main.rs", 3, 3, false), "main.rs, line 3");
39
  assert.equal(snippetTitle("main.rs", 1, 40, true), "main.rs");
40
  assert.equal(dedent("    fn a() {\n        b();\n\n    }"), "fn a() {\n    b();\n\n}", "shared indentation goes, blank lines stay");
41
});
42
43
test("a drop's line, embed and comments", () => {
44
  const d = { id: ID, owner: "andy", title: "", description: "", visibility: "secret", url: "", clone_url: "", created_ms: 0, updated_ms: 0, files: [{ name: "a.md", size: 3, raw_url: "" }], stars: 2, starred: false, forked_from: "/x/drops/y", embed_url: "https://hub/andy/drops/x/embed" };
45
  assert.deepEqual(dropLabel(d), { label: "a.md", description: "secret · 1 file · ★ 2 · fork" });
46
  assert.deepEqual(dropLabel({ ...d, title: "Notes", visibility: "public", stars: 0, forked_from: null, files: [...d.files, d.files[0]] }), { label: "Notes", description: "2 files" });
47
  assert.equal(embedSnippet({ ...d, title: 'say "hi"' }), '<iframe src="https://hub/andy/drops/x/embed" title="say &quot;hi&quot;" width="100%" height="400" style="border:0" loading="lazy"></iframe>');
48
  const line = commentLine({ id: 1, author: "sky", body: "nice\n\nthanks", created_ms: 0 }, 10_000);
49
  assert.deepEqual(line, { label: "sky", description: "just now", detail: "nice thanks" });
50
});