aboutsummaryrefslogtreecommitdiff
path: root/src/scraper.test.ts
blob: 236b0f53b9ad472725482964e1e1a234e33e55dd (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import assert from "node:assert";
import { describe, it } from "node:test";
import {
  getGitHubBlob,
  getGitHubCommits,
  getGitHubIssues,
  getGitHubRelease,
  getGitHubReleases,
  getGitHubRepo,
  getGitHubTree,
  GitHubHTTPError,
  RedirectError,
} from "./scraper.ts";

describe("GitHub scraper", () => {
  describe("actions/deploy-pages repository", () => {
    it("should fetch repository", async () => {
      const data = await getGitHubRepo("actions", "deploy-pages");

      assert.strictEqual(data.repo.owner, "actions");
      assert.strictEqual(data.repo.name, "deploy-pages");
      assert.strictEqual(data.branch, "main");
      assert.strictEqual(data.path, "/");

      assert.strictEqual(
        data.info.description,
        "GitHub Action to publish artifacts to GitHub Pages for deployments",
      );
      assert.strictEqual(data.info.website, "https://pages.github.com");
      assert.strictEqual(data.info.stars?.length, 3);
      assert.strictEqual(data.info.watchers?.length, 2);
      assert.strictEqual(data.info.forks?.length, 3);
      assert.strictEqual(data.info.numReleases, 38);
      assert.strictEqual(data.info.latestReleaseTag, "v5.0.0");
      assert.ok(data.info.latestReleaseDate?.startsWith("2026-03-25"));

      assert.ok(data.items.length > 0);
      assert.ok(Object.keys(data.overviewHtml ?? {}).includes("README.md"));
    });

    it("should fetch tree", async () => {
      const data = await getGitHubTree("github", "rally", "main", "lib");

      assert.strictEqual(data.repo.owner, "github");
      assert.strictEqual(data.repo.name, "rally");
      assert.strictEqual(data.branch, "main");
      assert.strictEqual(data.path, "lib");

      assert.deepStrictEqual(data.items, [
        {
          name: "README.md",
          path: "lib/README.md",
          contentType: "file",
        },
        {
          name: "RallyValidate.js",
          path: "lib/RallyValidate.js",
          contentType: "file",
        },
      ]);

      assert.ok(Object.keys(data.overviewHtml ?? {}).includes("README.md"));
    });

    it("should fetch text blob", async () => {
      const data = await getGitHubBlob("actions", "deploy-pages", "main", "LICENSE");

      assert.strictEqual(data.repo.owner, "actions");
      assert.strictEqual(data.repo.name, "deploy-pages");
      assert.strictEqual(data.branch, "main");
      assert.strictEqual(data.path, "LICENSE");

      assert.strictEqual(data.size, "1.04 KB / 21 lines / 17 loc");
      assert.strictEqual(data.language, "Text");
      assert.strictEqual(data.image, false);

      assert.strictEqual(data.textLines?.length, 21);
      assert.strictEqual(data.textLines[0], "MIT License");
      assert.strictEqual(data.htmlLines?.length, 21);
      assert.strictEqual(data.htmlLines[0], "MIT License");
      assert.strictEqual(data.htmlContent, null);
    });

    it("should fetch Markdown blob", async () => {
      const data = await getGitHubBlob("actions", "deploy-pages", "main", "README.md");

      assert.strictEqual(data.repo.owner, "actions");
      assert.strictEqual(data.repo.name, "deploy-pages");
      assert.strictEqual(data.branch, "main");
      assert.strictEqual(data.path, "README.md");

      assert.strictEqual(data.size, "8.95 KB / 133 lines / 94 loc");
      assert.strictEqual(data.language, "Markdown");
      assert.strictEqual(data.image, false);

      assert.strictEqual(data.textLines?.[0], "# deploy-pages 🚀");
      assert.strictEqual(
        data.htmlLines?.[0],
        '<span class="pl-mh"># <span class="pl-en">deploy-pages 🚀</span></span>',
      );
      const firstLine =
        /^<article class="markdown-body entry-content container-lg" itemprop="text"><div class="markdown-heading" dir="auto"><h1 tabindex="-1" class="heading-element" dir="auto">deploy-pages 🚀<\/h1>/;
      assert.match(data.htmlContent ?? "", firstLine);
    });

    it("should fetch code blob", async () => {
      const data = await getGitHubBlob("actions", "deploy-pages", "main", ".gitattributes");

      assert.strictEqual(data.repo.owner, "actions");
      assert.strictEqual(data.repo.name, "deploy-pages");
      assert.strictEqual(data.branch, "main");
      assert.strictEqual(data.path, ".gitattributes");

      assert.strictEqual(data.size, "39 Bytes / 1 lines / 1 loc");
      assert.strictEqual(data.language, "Git Attributes");
      assert.strictEqual(data.image, false);

      assert.deepStrictEqual(data.textLines, ["dist/** -diff linguist-generated=true "]);
      assert.deepStrictEqual(data.htmlLines, [
        '\u003cspan class="pl-e"\u003edist\u003c/span\u003e/\u003cspan class="pl-k"\u003e**\u003c/span\u003e \u003cspan class="pl-k"\u003e-\u003c/span\u003e\u003cspan class="pl-v"\u003ediff\u003c/span\u003e \u003cspan class="pl-v"\u003elinguist-generated\u003c/span\u003e=\u003cspan class="pl-c1"\u003etrue\u003c/span\u003e ',
      ]);
      assert.strictEqual(data.htmlContent, null);
    });

    it("should fetch issues", async () => {
      const data = await getGitHubIssues("actions", "deploy-pages");

      assert.strictEqual(data.repo.owner, "actions");
      assert.strictEqual(data.repo.name, "deploy-pages");
      assert.strictEqual(data.q, undefined);

      const issue402 = data.issues.find((issue) => issue.number === 402);
      assert.ok(issue402);
      assert.strictEqual(issue402.title, "Dry Run");
      assert.strictEqual(issue402.state, "OPEN");
      assert.strictEqual(issue402.createdAt, "2025-07-03T19:07:08Z");
    });

    it("should fetch filtered issues with q", async () => {
      const data = await getGitHubIssues("actions", "deploy-pages", {
        q: "is:issue state:closed",
      });

      assert.strictEqual(data.repo.owner, "actions");
      assert.strictEqual(data.repo.name, "deploy-pages");
      assert.strictEqual(data.q, "is:issue state:closed");
      const issue417 = data.issues.find((issue) => issue.number === 417);
      assert.ok(issue417);
      assert.strictEqual(issue417.title, "Chats");
      assert.ok(data.issues.every((issue) => issue.title !== ""));
    });

    it("should fetch commits", async () => {
      const data = await getGitHubCommits("actions", "deploy-pages", "main", "");

      assert.strictEqual(data.repo.owner, "actions");
      assert.strictEqual(data.repo.name, "deploy-pages");
      assert.strictEqual(data.branch, "main");
      assert.strictEqual(data.path, "");

      assert.ok(data.commitGroups.length > 0);
      const firstGroup = data.commitGroups[0];
      assert.ok(firstGroup.title.length > 0);
      assert.ok(firstGroup.commits.length > 0);

      const firstCommit = firstGroup.commits[0];
      assert.ok(firstCommit.oid.length === 40);
      assert.ok(firstCommit.shortMessage.length > 0);
      assert.ok(firstCommit.authors.length > 0);
    });

    it("should fetch commits with path", async () => {
      const data = await getGitHubCommits("actions", "deploy-pages", "main", "src");

      assert.strictEqual(data.repo.owner, "actions");
      assert.strictEqual(data.repo.name, "deploy-pages");
      assert.strictEqual(data.branch, "main");
      assert.strictEqual(data.path, "src");

      assert.ok(data.commitGroups.length > 0);
    });

    it("should fetch releases", async () => {
      const data = await getGitHubReleases("actions", "deploy-pages");

      assert.strictEqual(data.repo.owner, "actions");
      assert.strictEqual(data.repo.name, "deploy-pages");

      assert.ok(data.releases.length > 0);
      const firstRelease = data.releases[0];
      assert.ok(firstRelease.tagName.length > 0);
      assert.ok(firstRelease.title.match(/^v\d/));
      assert.ok(firstRelease.publishedAt.length > 0);
      assert.ok(firstRelease.bodyHtml.length > 0);
    });

    it("should fetch single release", async () => {
      const data = await getGitHubRelease("actions", "deploy-pages", "v4.0.5");

      assert.strictEqual(data.repo.owner, "actions");
      assert.strictEqual(data.repo.name, "deploy-pages");

      assert.ok(data.release.tagName.length > 0);
      assert.ok(data.release.title.length > 0);
      assert.ok(data.release.publishedAt.length > 0);
      assert.ok(data.release.bodyHtml.length > 0);
      assert.ok(data.release.assets && data.release.assets.length >= 2);
      assert.strictEqual(data.release.assets[0].name, "Source code (zip)");
      assert.strictEqual(
        data.release.assets[0].url,
        "https://github.com/actions/deploy-pages/archive/refs/tags/v4.0.5.zip",
      );
    });
  });

  describe("dustinlyons/nixos-config repository", () => {
    it("should fetch nix blob with combined overlapping styling directives", async () => {
      const data = await getGitHubBlob("dustinlyons", "nixos-config", "main", "flake.nix");

      assert.strictEqual(data.repo.owner, "dustinlyons");
      assert.strictEqual(data.repo.name, "nixos-config");
      assert.strictEqual(data.path, "flake.nix");
      assert.strictEqual(data.image, false);
      assert.strictEqual(data.htmlContent, null);
      assert.ok(data.htmlLines !== null && data.htmlLines.length > 0);

      // Line 2 has directives [2,13,"pl-c1"] and [2,13,"pl-s1"] with identical ranges.
      // They should be combined into one span, not duplicated.
      const line2 = data.htmlLines[1];
      assert.ok(line2.includes('class="pl-c1 pl-s1"'), `expected combined classes in: ${line2}`);
      assert.ok(
        (line2.match(/description/g) ?? []).length === 1,
        `expected "description" to appear once in: ${line2}`,
      );
    });
  });

  describe("rich file types", () => {
    it("should support PNG", async () => {
      const data = await getGitHubBlob("github", "docs", "main", "assets/images/site/logo.png");

      assert.strictEqual(data.repo.owner, "github");
      assert.strictEqual(data.repo.name, "docs");
      assert.strictEqual(data.branch, "main");
      assert.strictEqual(data.path, "assets/images/site/logo.png");

      assert.strictEqual(data.size, "1.26 KB");
      assert.strictEqual(data.language, null);
      assert.strictEqual(data.image, true);
      assert.strictEqual(data.textLines, null);
      assert.strictEqual(data.htmlLines, null);
      assert.strictEqual(data.htmlContent, null);
    });
  });

  describe("error handling", () => {
    it("should throw for non-existent repository", async () => {
      await assert.rejects(getGitHubRepo("nosuchowner", "nosuchrepo"), (err) => {
        assert(err instanceof GitHubHTTPError, "error should be an HTTPError");
        assert.strictEqual(err.status, 404);
        return true;
      });
    });

    it("should redirect when fetching a directory as a blob", async () => {
      await assert.rejects(
        getGitHubBlob("earendil-works", "pi", "main", "packages/agent"),
        (err) => {
          assert(err instanceof RedirectError, "error should be a RedirectError");
          assert.strictEqual(err.location, "/earendil-works/pi/tree/main/packages/agent");
          return true;
        },
      );
    });

    it("should redirect when fetching a file as a tree", async () => {
      await assert.rejects(
        getGitHubTree("earendil-works", "pi", "main", "packages/agent/package.json"),
        (err) => {
          assert(err instanceof RedirectError, "error should be a RedirectError");
          assert.strictEqual(
            err.location,
            "/earendil-works/pi/blob/main/packages/agent/package.json",
          );
          return true;
        },
      );
    });
  });
});