summaryrefslogtreecommitdiff
path: root/cgithub/src/app.test.ts
blob: e28c3752b19fbb6ebb4a13de1548bc33fd305691 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import assert from "node:assert";
import path from "node:path";
import { describe, it, type TestContext } 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("sidebar fragment", () => {
  const repo = { owner: "actions", name: "deploy-pages" };
  const languages = [{ name: "JavaScript", percentage: 100, color: "#f1e05a" }];
  const latestRelease = { tag: "v5.0.0", publishedAt: "2026-03-25T16:59:14Z" };

  // The release replaces the empty element it names, beside the release count.
  it("should render the latest release and the languages", () => {
    const html = eta.render("_sidebar.eta", { repo, languages, latestRelease });

    assert.match(html, /<span data-target="latest-release">\(<a href/);
    assert.match(html, /href="\/actions\/deploy-pages\/releases\/tag\/v5.0.0"/);
    assert.match(html, /<time class="timestamp" title="2026-03-25T16:59:14Z">2026-03-25<\/time>/);
    assert.match(html, /<div class="languages box">/);
  });

  it("should name a spot the repo page holds open", () => {
    const html = eta.render("repo.eta", {
      repo,
      branch: "main",
      path: "/",
      items: [],
      info: { description: null, website: null, stars: null, forks: null, numReleases: 38 },
    });

    assert.match(html, /<span id="latest-release"><\/span>/);
  });

  it("should render nothing when the repository has neither", () => {
    const html = eta.render("_sidebar.eta", { repo, languages: [], latestRelease: null });

    assert.strictEqual(html.trim(), "");
  });
});

describe("blob view", () => {
  const blob = {
    repo: { owner: "justrajdeep", name: "fonts" },
    branch: "master",
    path: "Times New Roman.ttf",
    language: null,
    size: "815 KB",
    image: false,
    renderFileType: null,
    textLines: null,
    htmlLines: null,
    htmlContent: null,
  };

  it("should embed a PDF", () => {
    const html = eta.render("blob.eta", {
      ...blob,
      path: "test/pdfs/basicapi.pdf",
      renderFileType: "pdf",
    });

    assert.match(html, /<object[^>]*type="application\/pdf"/);
    assert.match(html, /data="\/justrajdeep\/fonts\/embed\/pdf\/master\/test\/pdfs\/basicapi.pdf"/);
  });

  it("should not embed a file type it has no viewer for", () => {
    const html = eta.render("blob.eta", { ...blob, renderFileType: "stl" });

    assert.doesNotMatch(html, /<object/);
    assert.match(html, /This file cannot be displayed\./);
  });

  it("should offer the raw file when there is nothing to display", () => {
    const html = eta.render("blob.eta", blob);

    assert.match(html, /This file cannot be displayed\./);
    assert.match(html, /href="\/justrajdeep\/fonts\/raw\/master\/Times New Roman.ttf">View raw/);
  });
});

describe("embedded files", () => {
  function stubGitHub(t: TestContext, body: string, contentType: string, status = 200) {
    t.mock.method(
      globalThis,
      "fetch",
      async () => new Response(body, { status, headers: { "Content-Type": contentType } }),
    );
  }

  it("should retype a PDF GitHub sends as a download", async (t) => {
    stubGitHub(t, "%PDF-1.6", "application/octet-stream");

    const res = await app.request("http://cgithub.example/mozilla/pdf.js/embed/pdf/master/doc.pdf");

    assert.strictEqual(res.status, 200);
    assert.strictEqual(res.headers.get("Content-Type"), "application/pdf");
    assert.strictEqual(await res.text(), "%PDF-1.6");
  });

  it("should pass on a type GitHub already got right", async (t) => {
    stubGitHub(t, "%PDF-1.6", "application/pdf");

    const res = await app.request("http://cgithub.example/mozilla/pdf.js/embed/pdf/master/doc.pdf");

    assert.strictEqual(res.headers.get("Content-Type"), "application/pdf");
  });

  it("should not serve a file type it has no viewer for", async (t) => {
    stubGitHub(t, "<script>", "text/html");

    const res = await app.request("http://cgithub.example/mozilla/pdf.js/embed/html/master/x.html");

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

  it("should pass on the status of a file GitHub does not have", async (t) => {
    stubGitHub(t, "404: Not Found", "text/plain", 404);

    const res = await app.request(
      "http://cgithub.example/mozilla/pdf.js/embed/pdf/master/nope.pdf",
    );

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

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/);
  });

  // Same-origin requests are mostly subresources, which render an HTML page as
  // a broken image rather than following its refresh.
  it("should redirect over HTTP when the referer is one of our own pages", async () => {
    const res = await app.request("http://cgithub.example/a/b/c?x=1", {
      headers: { Referer: "http://cgithub.example/a/b" },
    });

    assert.strictEqual(res.status, 302);
    assert.strictEqual(res.headers.get("location"), "https://github.com/a/b/c?x=1");
  });

  it("should use a meta refresh when the referer is another site", async () => {
    const res = await app.request("http://cgithub.example/a/b/c?x=1", {
      headers: { Referer: "https://github.example/a/b" },
    });

    assert.strictEqual(res.status, 200);
    assert.strictEqual(res.headers.get("location"), null);
  });

  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"/,
    );
  });
});