-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgithub.spec.ts
More file actions
90 lines (74 loc) · 3.38 KB
/
github.spec.ts
File metadata and controls
90 lines (74 loc) · 3.38 KB
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
// Import Node.js Dependencies
import { test, after, describe, it } from "node:test";
import assert from "node:assert";
import fs from "node:fs";
// Import Third-party Dependencies
import { MockAgent, setGlobalDispatcher } from "undici";
import type { Repository } from "@dashlog/fetch-github-repositories";
// Import Internal Dependencies
import Github from "../src/services/github.ts";
const kMaxCommitFetch = 60;
const nsGithubOrg = JSON.parse(fs.readFileSync("./test/fixtures/nodesecure-github-org.json", "utf-8"));
const repositories = JSON.parse(
fs.readFileSync("./test/fixtures/repos.json", "utf-8")
) as Repository[];
const issues = JSON.parse(fs.readFileSync("./test/fixtures/issues.json", "utf-8"));
const pulls = JSON.parse(fs.readFileSync("./test/fixtures/pulls.json", "utf-8"));
const commits = JSON.parse(fs.readFileSync("./test/fixtures/commits.json", "utf-8"));
const packageJson = JSON.parse(fs.readFileSync("./test/fixtures/package.json", "utf-8"));
const repositoryNames = repositories.map((repo) => repo.name);
describe("Github", async() => {
const mockAgent = new MockAgent();
// TODO: Node 18 LTS (18.15.0) will run tests suites BEFORE the before() hook
// It is fixed in v19 (tested with 19.8.1)
// Wrap this is in before() hook when fixed in v18.
mockAgent.disableNetConnect();
const mockPool = mockAgent.get("https://api.github.com");
mockPool.intercept({ path: "/orgs/NodeSecure" }).reply(200, nsGithubOrg, {
headers: { "Content-Type": "application/json" }
}).persist();
mockPool.intercept({ path: "/orgs/NodeSecure/repos" }).reply(200, repositories, {
headers: { "Content-Type": "application/json" }
}).persist();
for (const repo of repositoryNames) {
mockPool.intercept({ path: `/repos/NodeSecure/${repo}/pulls` }).reply(200, pulls, {
headers: { "Content-Type": "application/json" }
}).persist();
mockPool.intercept({ path: `/repos/NodeSecure/${repo}/issues` }).reply(200, issues, {
headers: { "Content-Type": "application/json" }
}).persist();
mockPool.intercept({ path: `/repos/NodeSecure/${repo}/commits?per_page=${kMaxCommitFetch}` }).reply(200, commits, {
headers: { "Content-Type": "application/json" }
}).persist();
}
const rawPool = mockAgent.get("https://raw.githubusercontent.com");
rawPool.intercept({
path: /\/.*/
}).reply(200, packageJson).persist();
setGlobalDispatcher(mockAgent);
// End of before() hook
after(async() => {
await mockAgent.close();
});
test("Github.information()", async() => {
const github = new Github("NodeSecure");
const data = await github.information();
assert.deepEqual(data, nsGithubOrg);
});
it("Github.fetchRepositories()", async() => {
const github = new Github("NodeSecure");
const data = await github.fetchRepositories();
const noArchivedOrDisabled = repositories.filter((repo) => !repo.archived && !repo.disabled);
for (const repo of data) {
assert.ok(noArchivedOrDisabled.find((r) => r.name === repo.name));
}
});
it("@nodesecure/cli should have 30 unreleased commit from 09 Nov 2022", async() => {
const github = new Github("NodeSecure");
const data = await github.fetchRepositories();
const cli = data.find((repo) => repo.name === "cli")!;
const dateWithoutTime = cli.last_release!.split(",")[0];
assert.equal(cli.unreleased_commit_count, 30);
assert.equal(dateWithoutTime, "09 Nov 2022");
});
});