/
load.test.js
225 lines (209 loc) · 8.23 KB
/
load.test.js
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
// @flow
import deepFreeze from "deep-freeze";
import tmp from "tmp";
import path from "path";
import fs from "fs-extra";
import type {Options as LoadGraphOptions} from "../plugins/github/loadGraph";
import type {Options as LoadDiscourseOptions} from "../plugins/discourse/loadDiscourse";
import type {Project} from "../core/project";
import {
directoryForProjectId,
getProjectIds,
loadProject,
} from "../core/project_io";
import {makeRepoId} from "../core/repoId";
import {defaultWeights} from "../analysis/weights";
import {NodeAddress, Graph} from "../core/graph";
import {node} from "../core/graphTestUtil";
import {TestTaskReporter} from "../util/taskReporter";
import {load, type LoadOptions} from "./load";
import {
type PartialTimelineCredParameters,
partialParams,
} from "../analysis/timeline/params";
type JestMockFn = $Call<typeof jest.fn>;
jest.mock("../plugins/github/loadGraph", () => ({
loadGraph: jest.fn(),
}));
const loadGraph: JestMockFn = (require("../plugins/github/loadGraph")
.loadGraph: any);
jest.mock("../plugins/discourse/loadDiscourse", () => ({
loadDiscourse: jest.fn(),
}));
const loadDiscourse: JestMockFn = (require("../plugins/discourse/loadDiscourse")
.loadDiscourse: any);
jest.mock("../analysis/timeline/timelineCred", () => ({
TimelineCred: {compute: jest.fn()},
}));
const timelineCredCompute: JestMockFn = (require("../analysis/timeline/timelineCred")
.TimelineCred.compute: any);
describe("api/load", () => {
const fakeTimelineCred = deepFreeze({
toJSON: () => ({is: "fake-timeline-cred"}),
});
const githubSentinel = node("github-sentinel");
const githubGraph = () => new Graph().addNode(githubSentinel);
const discourseSentinel = node("discourse-sentinel");
const discourseGraph = () => new Graph().addNode(discourseSentinel);
const combinedGraph = () => Graph.merge([githubGraph(), discourseGraph()]);
beforeEach(() => {
jest.clearAllMocks();
loadGraph.mockResolvedValue(githubGraph());
loadDiscourse.mockResolvedValue(discourseGraph());
timelineCredCompute.mockResolvedValue(fakeTimelineCred);
});
const discourseServerUrl = "https://example.com";
const discourseApiUsername = "credbot";
const project: Project = {
id: "foo",
repoIds: [makeRepoId("foo", "bar")],
discourseServer: {
serverUrl: discourseServerUrl,
apiUsername: discourseApiUsername,
},
};
deepFreeze(project);
const githubToken = "EXAMPLE_TOKEN";
const discourseKey = "EXAMPLE_KEY";
const weights = defaultWeights();
// Tweaks the weights so that we can ensure we aren't overriding with default weights
weights.nodeManualWeights.set(NodeAddress.empty, 33);
// Deep freeze will freeze the weights, too
const params: PartialTimelineCredParameters = {weights};
const plugins = deepFreeze([]);
const example = () => {
const sourcecredDirectory = tmp.dirSync().name;
const taskReporter = new TestTaskReporter();
const options: LoadOptions = {
sourcecredDirectory,
githubToken,
params,
plugins,
project,
discourseKey,
};
return {options, taskReporter, sourcecredDirectory};
};
it("sets up a project directory for the project", async () => {
const {options, taskReporter, sourcecredDirectory} = example();
await load(options, taskReporter);
expect(await getProjectIds(sourcecredDirectory)).toEqual([project.id]);
expect(await loadProject(project.id, sourcecredDirectory)).toEqual(project);
});
it("calls github loadGraph with the right options", async () => {
const {options, taskReporter, sourcecredDirectory} = example();
await load(options, taskReporter);
const cacheDirectory = path.join(sourcecredDirectory, "cache");
const expectedLoadGraphOptions: LoadGraphOptions = {
repoIds: project.repoIds,
token: githubToken,
cacheDirectory,
};
expect(loadGraph).toHaveBeenCalledWith(
expectedLoadGraphOptions,
taskReporter
);
});
it("calls loadDiscourse with the right options", async () => {
const {options, taskReporter, sourcecredDirectory} = example();
await load(options, taskReporter);
const cacheDirectory = path.join(sourcecredDirectory, "cache");
const expectedOptions: LoadDiscourseOptions = {
fetchOptions: {
apiUsername: discourseApiUsername,
apiKey: discourseKey,
serverUrl: discourseServerUrl,
},
cacheDirectory,
};
expect(loadDiscourse).toHaveBeenCalledWith(expectedOptions, taskReporter);
});
it("saves a merged graph to disk", async () => {
const {options, taskReporter, sourcecredDirectory} = example();
await load(options, taskReporter);
const projectDirectory = directoryForProjectId(
project.id,
sourcecredDirectory
);
const graphFile = path.join(projectDirectory, "graph.json");
const graphJSON = JSON.parse(await fs.readFile(graphFile));
const expectedJSON = combinedGraph().toJSON();
expect(graphJSON).toEqual(expectedJSON);
});
it("calls TimelineCred.compute with the right graph and options", async () => {
const {options, taskReporter} = example();
await load(options, taskReporter);
const args = timelineCredCompute.mock.calls[0][0];
expect(args.graph.equals(combinedGraph())).toBe(true);
expect(args.params).toEqual(partialParams(params));
expect(args.plugins).toEqual(plugins);
});
it("saves the resultant cred.json to disk", async () => {
const {options, taskReporter, sourcecredDirectory} = example();
await load(options, taskReporter);
const projectDirectory = directoryForProjectId(
project.id,
sourcecredDirectory
);
const credFile = path.join(projectDirectory, "cred.json");
const credJSON = JSON.parse(await fs.readFile(credFile));
expect(credJSON).toEqual(fakeTimelineCred.toJSON());
});
it("gives the right tasks to the TaskReporter", async () => {
const {options, taskReporter} = example();
await load(options, taskReporter);
expect(taskReporter.activeTasks()).toEqual([]);
expect(taskReporter.entries()).toEqual([
{type: "START", taskId: "load-foo"},
{type: "START", taskId: "compute-cred"},
{type: "FINISH", taskId: "compute-cred"},
{type: "FINISH", taskId: "load-foo"},
]);
});
it("errors if a discourse server is provided without a discourse key", () => {
const {options, taskReporter} = example();
const optionsWithoutKey = {...options, discourseKey: null};
expect.assertions(1);
return load(optionsWithoutKey, taskReporter).catch((e) =>
expect(e.message).toMatch("no Discourse key")
);
});
it("errors if GitHub repoIds are provided without a GitHub token", () => {
const {options, taskReporter} = example();
const optionsWithoutToken = {...options, githubToken: null};
expect.assertions(1);
return load(optionsWithoutToken, taskReporter).catch((e) =>
expect(e.message).toMatch("no GitHub token")
);
});
it("only loads GitHub if no Discourse server set", async () => {
const {options, taskReporter, sourcecredDirectory} = example();
const newProject = {...options.project, discourseServer: null};
const newOptions = {...options, project: newProject, discourseKey: null};
await load(newOptions, taskReporter);
expect(loadDiscourse).not.toHaveBeenCalled();
const projectDirectory = directoryForProjectId(
project.id,
sourcecredDirectory
);
const graphFile = path.join(projectDirectory, "graph.json");
const graphJSON = JSON.parse(await fs.readFile(graphFile));
const expectedJSON = githubGraph().toJSON();
expect(graphJSON).toEqual(expectedJSON);
});
it("only loads Discourse if no GitHub repoIds set ", async () => {
const {options, taskReporter, sourcecredDirectory} = example();
const newProject = {...options.project, repoIds: []};
const newOptions = {...options, project: newProject, githubToken: null};
await load(newOptions, taskReporter);
expect(loadGraph).not.toHaveBeenCalled();
const projectDirectory = directoryForProjectId(
project.id,
sourcecredDirectory
);
const graphFile = path.join(projectDirectory, "graph.json");
const graphJSON = JSON.parse(await fs.readFile(graphFile));
const expectedJSON = discourseGraph().toJSON();
expect(graphJSON).toEqual(expectedJSON);
});
});