summaryrefslogtreecommitdiff
path: root/cgithub/src/app.test.ts
blob: 6544f0313ba9e984731755ebc13b9acfdbdaf578 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import assert from "node:assert";
import path from "node:path";
import { describe, it } from "node:test";
import { Eta } from "eta";
import { createApp } from "./app.ts";

// The routes exercised here redirect without scraping, so no network is needed.
const eta = new Eta({ views: path.join(import.meta.dirname, "..", "views") });
const app = createApp(eta);

describe("branch picker", () => {
  const repo = { owner: "actions", name: "deploy-pages" };
  const refs = [{ name: "main", isDefault: true }, { name: "v4" }];

  it("should keep the current view and path", () => {
    const blob = eta.render("_refs.eta", {
      repo,
      refs,
      type: "branches",
      view: "blob",
      path: "src/main.ts",
    });
    const tree = eta.render("_refs.eta", {
      repo,
      refs,
      type: "branches",
      view: "tree",
      path: "src",
    });

    assert.match(blob, /href="\/actions\/deploy-pages\/blob\/main\/src\/main.ts"/);
    assert.match(blob, /href="\/actions\/deploy-pages\/blob\/v4\/src\/main.ts"/);
    assert.match(tree, /href="\/actions\/deploy-pages\/tree\/v4\/src"/);
  });

  it("should link to the root tree without a path", () => {
    const html = eta.render("_refs.eta", { repo, refs, type: "branches", view: "tree", path: "" });

    assert.match(html, /href="\/actions\/deploy-pages\/tree\/main"/);
  });

  it("should not build ref links for views it does not serve", async () => {
    const res = await app.request("http://cgithub.example/api/a/b/refs/branches/raw/README.md");

    assert.strictEqual(res.status, 404);
  });

  it("should tell the refs endpoint where the picker was opened", () => {
    const nav = { repo, branch: "main", path: "src" };

    const blob = eta.render("_nav.eta", { ...nav, view: "blob", path: "src/main.ts" });
    assert.match(blob, /data-view="blob" data-path="src\/main.ts"/);
    assert.match(
      eta.render("_nav.eta", { ...nav, view: "tree" }),
      /data-view="tree" data-path="src"/,
    );
    assert.doesNotMatch(eta.render("_nav.eta", { ...nav, view: "tree", path: "" }), /data-path=/);
  });
});

describe("redirects to GitHub", () => {
  // An HTTP redirect would be caught by the extension's declarativeNetRequest
  // rules and bounced straight back here.
  it("should redirect unknown paths with a meta refresh", async () => {
    const res = await app.request("http://cgithub.example/a/b/c?x=1");

    assert.strictEqual(res.status, 200);
    assert.strictEqual(res.headers.get("location"), null);
    assert.strictEqual(res.headers.get("Referrer-Policy"), "no-referrer");

    const body = await res.text();
    assert.match(
      body,
      /<meta http-equiv="refresh" content="1; url=https:\/\/github.com\/a\/b\/c\?x=1">/,
    );
    assert.doesNotMatch(body, /<script/);
  });

  it("should redirect unhandled search types", async () => {
    const res = await app.request(
      "http://cgithub.example/actions/deploy-pages/search?q=x&type=code",
    );

    assert.strictEqual(res.status, 200);
    assert.match(
      await res.text(),
      /https:\/\/github.com\/actions\/deploy-pages\/search\?q=x&amp;type=code/,
    );
  });

  it("should redirect raw file requests to raw.githubusercontent.com", async () => {
    const res = await app.request("http://cgithub.example/actions/deploy-pages/raw/main/README.md");

    assert.strictEqual(res.status, 200);
    assert.match(
      await res.text(),
      /url=https:\/\/raw.githubusercontent.com\/actions\/deploy-pages\/main\/README.md"/,
    );
  });
});