OXYGENHUB
# 95f69b4a3ac2 oxygenhub-vscode / test / hub.test.mjs
5.9 KiB · last changed Notifications in the status bar and an inbox (0.3.0) 1 month ago source blame history raw
1
// Against a real hub when the binary exists: create a repository, an
2
// issue and a proposal through the client.
3
import { test } from "node:test";
4
import assert from "node:assert/strict";
5
import { spawn, execFileSync } from "node:child_process";
6
import { mkdtempSync, writeFileSync, existsSync, mkdirSync } from "node:fs";
7
import { tmpdir } from "node:os";
8
import { join } from "node:path";
9
import { HubClient, HubError, encodeDiffQuery, decodeDiffQuery, hubTarget } from "../out/hub.mjs";
10
11
const HUB = join(import.meta.dirname, "../../oxygenhub/target/release/oxygenhub");
12
const OXY = join(import.meta.dirname, "../../oxygen/target/release/oxy");
13
14
test("hub client end to end", { skip: !existsSync(HUB) || !existsSync(OXY) }, async () => {
15
  const data = mkdtempSync(join(tmpdir(), "hubvs-"));
16
  const port = 17300 + Math.floor(Math.random() * 1000);
17
  const base = `http://127.0.0.1:${port}`;
18
  writeFileSync(join(data, "hub.toml"), `[server]\nbind = "127.0.0.1:${port}"\nbase_url = "${base}"\n[auth]\npush_tokens = ["site"]\n[registration]\nmode = "open"\n`);
19
  execFileSync(HUB, ["--data", data, "init"], { stdio: "ignore" });
20
  execFileSync(HUB, ["--data", data, "admin", "user", "create", "andy", "--admin"], { stdio: "ignore" });
21
  const proc = spawn(HUB, ["--data", data, "serve"], { stdio: "ignore" });
22
  try {
23
    for (let i = 0; i < 50; i++) {
24
      try {
25
        await fetch(`${base}/health`);
26
        break;
27
      } catch {
28
        await new Promise((r) => setTimeout(r, 100));
29
      }
30
    }
31
    const client = new HubClient(base, "site");
32
    // The client has no createRepository method by design; use fetch directly.
33
    const res = await fetch(`${base}/api/v1/repos`, { method: "POST", headers: { authorization: "Bearer site", "content-type": "application/json" }, body: JSON.stringify({ owner: "andy", name: "demo" }) });
34
    assert.equal(res.status, 201);
35
    // Push a bookmark with oxy so a proposal has something to add.
36
    const work = mkdtempSync(join(tmpdir(), "hubvs-work-"));
37
    const env = { ...process.env, OXY_HOME: mkdtempSync(join(tmpdir(), "hubvs-home-")) };
38
    const oxy = (...args) => execFileSync(OXY, ["--plain", ...args], { cwd: work, env, stdio: ["ignore", "pipe", "pipe"] }).toString();
39
    oxy("init");
40
    oxy("remote", "add", "origin", `${base}/andy/demo`);
41
    oxy("config", "remote.origin.token", "site");
42
    mkdirSync(join(work, "src"), { recursive: true });
43
    writeFileSync(join(work, "src/a.txt"), "a\n");
44
    oxy("commit", "-m", "base");
45
    oxy("push", "origin");
46
    oxy("bookmark", "create", "feat");
47
    oxy("bookmark", "switch", "feat");
48
    writeFileSync(join(work, "src/b.txt"), "b\n");
49
    writeFileSync(join(work, "src/a.txt"), "a\nA\n");
50
    oxy("commit", "-m", "feat");
51
    oxy("push", "origin", "--bookmark", "feat");
52
    const p = await client.createProposal("andy", "demo", { title: "Feat", target: "main", source: "feat", body: "", draft: false });
53
    assert.equal(p.number, 1);
54
    assert.equal(p.changes.length, 1);
55
    // The proposal's files and both sides, as the diff editors fetch them.
56
    const files = await client.proposalFiles("andy", "demo", 1);
57
    assert.deepEqual(
58
      files.map((f) => [f.path, f.kind]),
59
      [
60
        ["src/a.txt", "modified"],
61
        ["src/b.txt", "added"],
62
      ],
63
    );
64
    assert.equal(await client.proposalContent("andy", "demo", 1, "base", "src/a.txt"), "a\n");
65
    assert.equal(await client.proposalContent("andy", "demo", 1, "head", "src/a.txt"), "a\nA\n");
66
    assert.equal(await client.proposalContent("andy", "demo", 1, "base", "src/b.txt"), undefined, "absent on the target side");
67
    // A review comment from the diff comes back on its line.
68
    const c = await client.commentProposal("andy", "demo", 1, "why?", "src/a.txt", 2);
69
    assert.equal(c.line, 2);
70
    const comments = await client.proposalComments("andy", "demo", 1);
71
    assert.equal(comments[0].line, 2);
72
    const list = await client.proposals("andy", "demo");
73
    assert.equal(list[0].title, "Feat");
74
    const issue = await client.createIssue("andy", "demo", "Bug", "text");
75
    assert.equal(issue.number, 2);
76
    await client.commentIssue("andy", "demo", 2, "more");
77
    const closed = await client.setIssueState("andy", "demo", 2, "closed");
78
    assert.equal(closed.state, "closed");
79
    // The inbox: the site token acts as the administrator, andy.
80
    const inbox = await client.notifications();
81
    assert.equal(typeof inbox.unread, "number");
82
    assert.ok(Array.isArray(inbox.items));
83
    await client.markAllNotificationsRead();
84
    assert.equal((await client.notifications()).unread, 0);
85
    const me = await client.me();
86
    assert.equal(me.username, "andy");
87
    await assert.rejects(() => new HubClient(base, "nope").me(), (e) => e instanceof HubError && e.status === 401);
88
  } finally {
89
    proc.kill();
90
  }
91
});
92
93
test("diff document queries round trip", () => {
94
  const ref = { base: "https://earth.example:7343", owner: "andy", name: "demo", number: 7, side: "head", path: "src/dir with space/a.txt" };
95
  const back = decodeDiffQuery(encodeDiffQuery(ref), "/" + ref.path);
96
  assert.deepEqual(back, ref);
97
  assert.equal(decodeDiffQuery("hub=x&owner=a&name=b&number=0&side=head", "/p"), undefined, "number must be positive");
98
  assert.equal(decodeDiffQuery("hub=x&owner=a&name=b&number=3&side=left", "/p"), undefined, "side is base or head");
99
  assert.equal(decodeDiffQuery("owner=a&name=b&number=3&side=head", "/p"), undefined, "the hub is required");
100
});
101
102
test("hub page urls open in the editor when they are proposals or issues", () => {
103
  assert.deepEqual(hubTarget("https://earth.example:7343/andy/oxygen/proposal/12"), { kind: "proposal", base: "https://earth.example:7343", owner: "andy", name: "oxygen", number: 12 });
104
  assert.deepEqual(hubTarget("https://h/andy/demo/issue/3#c7"), { kind: "issue", base: "https://h", owner: "andy", name: "demo", number: 3 });
105
  assert.equal(hubTarget("https://h/andy/demo/releases/tag/v1").kind, "other");
106
  assert.equal(hubTarget("https://h/andy/demo/proposal/x").kind, "other");
107
});