Skip to content

Commit 6b5b974

Browse files
chore: refactor tests to use ConfigurationHandler and remove redundant getContent mocks
1 parent fb5c589 commit 6b5b974

7 files changed

Lines changed: 401 additions & 318 deletions

File tree

‎.github/workflows/bun-testing.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ jobs:
1515
permissions: write-all
1616
runs-on: ubuntu-latest
1717
steps:
18-
- uses: oven-sh/setup-bun@v1
19-
- uses: actions/setup-node@v4
18+
- uses: oven-sh/setup-bun@v2
19+
- uses: actions/setup-node@v5
2020
with:
2121
node-version: "20.10.0"
2222
- uses: actions/checkout@master

‎tests/commands.test.ts‎

Lines changed: 148 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
33
import { config } from "dotenv";
44
import { GitHubContext } from "../src/github/github-context";
55
import { GitHubEventHandler } from "../src/github/github-event-handler";
6-
import { CONFIG_FULL_PATH } from "../src/github/utils/config";
76
import { logger } from "../src/logger/logger";
87
import "./__mocks__/webhooks";
8+
import { createConfigurationHandler } from "./test-utils/configuration-handler";
99

1010
jest.mock("@octokit/plugin-paginate-rest", () => ({}));
1111
jest.mock("@octokit/plugin-rest-endpoint-methods", () => ({}));
@@ -17,6 +17,11 @@ config({ path: ".dev.vars" });
1717

1818
const name = "ubiquity-os-kernel";
1919
const eventName = "issue_comment.created";
20+
const fooDescription = "foo command";
21+
const barDescription = "bar command";
22+
const helloDescription = "This command says hello to the username provided in the parameters.";
23+
const helloExample = "/hello @pavlovcik";
24+
const helloUsernameDescription = "the user to say hello to";
2025

2126
afterEach(() => {
2227
jest.clearAllMocks();
@@ -31,60 +36,6 @@ const eventHandler = {
3136
logger: logger,
3237
} as unknown as GitHubEventHandler;
3338

34-
function getContent(params?: RestEndpointMethodTypes["repos"]["getContent"]["parameters"]) {
35-
if (params?.path === CONFIG_FULL_PATH) {
36-
return {
37-
data: `
38-
plugins:
39-
ubiquity-os/plugin-a: {}
40-
ubiquity-os/plugin-b: {}
41-
`,
42-
};
43-
} else if (params?.path === "manifest.json") {
44-
const manifest =
45-
params?.repo === "plugin-a"
46-
? {
47-
name: "plugin-A",
48-
homepage_url: "https://plugin-a.internal",
49-
commands: {
50-
foo: {
51-
description: "foo command",
52-
"ubiquity:example": "/foo bar",
53-
},
54-
bar: {
55-
description: "bar command",
56-
"ubiquity:example": "/bar foo",
57-
},
58-
},
59-
}
60-
: {
61-
name: "plugin-B",
62-
commands: {
63-
hello: {
64-
description: "This command says hello to the username provided in the parameters.",
65-
"ubiquity:example": "/hello @pavlovcik",
66-
parameters: {
67-
type: "object",
68-
properties: {
69-
username: {
70-
type: "string",
71-
description: "the user to say hello to",
72-
},
73-
},
74-
},
75-
},
76-
},
77-
};
78-
return {
79-
data: {
80-
content: Buffer.from(JSON.stringify(manifest)).toString("base64"),
81-
},
82-
};
83-
} else {
84-
throw new Error("Not found");
85-
}
86-
}
87-
8839
const payload = {
8940
repository: {
9041
owner: { login: "ubiquity" },
@@ -98,6 +49,49 @@ const payload = {
9849

9950
describe("Event related tests", () => {
10051
it("Should post the help menu", async () => {
52+
const configurationHandler = createConfigurationHandler({
53+
configuration: {
54+
plugins: {
55+
"ubiquity-os/plugin-a": { with: {} },
56+
"ubiquity-os/plugin-b": { with: {} },
57+
},
58+
},
59+
manifests: {
60+
"plugin-a": {
61+
name: "plugin-A",
62+
homepage_url: "https://plugin-a.internal",
63+
commands: {
64+
foo: {
65+
description: fooDescription,
66+
"ubiquity:example": "/foo bar",
67+
},
68+
bar: {
69+
description: barDescription,
70+
"ubiquity:example": "/bar foo",
71+
},
72+
},
73+
},
74+
"plugin-b": {
75+
name: "plugin-B",
76+
commands: {
77+
hello: {
78+
description: helloDescription,
79+
"ubiquity:example": helloExample,
80+
parameters: {
81+
type: "object",
82+
properties: {
83+
username: {
84+
type: "string",
85+
description: helloUsernameDescription,
86+
},
87+
},
88+
},
89+
},
90+
},
91+
},
92+
},
93+
});
94+
10195
const issues = {
10296
createComment(params?: RestEndpointMethodTypes["issues"]["createComment"]["parameters"]) {
10397
return params;
@@ -112,9 +106,6 @@ describe("Event related tests", () => {
112106
octokit: {
113107
rest: {
114108
issues,
115-
repos: {
116-
getContent: jest.fn(getContent),
117-
},
118109
},
119110
},
120111
openAi: {
@@ -143,6 +134,7 @@ describe("Event related tests", () => {
143134
},
144135
},
145136
eventHandler: eventHandler,
137+
configurationHandler,
146138
payload: {
147139
...payload,
148140
comment: {
@@ -167,6 +159,48 @@ describe("Event related tests", () => {
167159
});
168160

169161
it("Should call appropriate plugin", async () => {
162+
const configurationHandler = createConfigurationHandler({
163+
configuration: {
164+
plugins: {
165+
"ubiquity-os/plugin-a": { with: {} },
166+
"ubiquity-os/plugin-b": { with: {} },
167+
},
168+
},
169+
manifests: {
170+
"plugin-a": {
171+
name: "plugin-A",
172+
commands: {
173+
foo: {
174+
description: fooDescription,
175+
"ubiquity:example": "/foo bar",
176+
},
177+
bar: {
178+
description: barDescription,
179+
"ubiquity:example": "/bar foo",
180+
},
181+
},
182+
},
183+
"plugin-b": {
184+
name: "plugin-B",
185+
commands: {
186+
hello: {
187+
description: helloDescription,
188+
"ubiquity:example": helloExample,
189+
parameters: {
190+
type: "object",
191+
properties: {
192+
username: {
193+
type: "string",
194+
description: helloUsernameDescription,
195+
},
196+
},
197+
},
198+
},
199+
},
200+
},
201+
},
202+
});
203+
170204
const dispatchWorkflow = jest.fn();
171205
jest.mock("../src/github/utils/workflow-dispatch", () => ({
172206
getDefaultBranch: jest.fn().mockImplementation(() => Promise.resolve("main")),
@@ -187,9 +221,6 @@ describe("Event related tests", () => {
187221
octokit: {
188222
rest: {
189223
issues,
190-
repos: {
191-
getContent: jest.fn(getContent),
192-
},
193224
},
194225
},
195226
openAi: {
@@ -218,6 +249,7 @@ describe("Event related tests", () => {
218249
},
219250
},
220251
eventHandler: eventHandler,
252+
configurationHandler,
221253
payload: {
222254
...payload,
223255
comment: {
@@ -240,6 +272,48 @@ describe("Event related tests", () => {
240272
});
241273

242274
it("Should not answer with arbitrary requests", async () => {
275+
const configurationHandler = createConfigurationHandler({
276+
configuration: {
277+
plugins: {
278+
"ubiquity-os/plugin-a": { with: {} },
279+
"ubiquity-os/plugin-b": { with: {} },
280+
},
281+
},
282+
manifests: {
283+
"plugin-a": {
284+
name: "plugin-A",
285+
commands: {
286+
foo: {
287+
description: fooDescription,
288+
"ubiquity:example": "/foo bar",
289+
},
290+
bar: {
291+
description: barDescription,
292+
"ubiquity:example": "/bar foo",
293+
},
294+
},
295+
},
296+
"plugin-b": {
297+
name: "plugin-B",
298+
commands: {
299+
hello: {
300+
description: helloDescription,
301+
"ubiquity:example": helloExample,
302+
parameters: {
303+
type: "object",
304+
properties: {
305+
username: {
306+
type: "string",
307+
description: helloUsernameDescription,
308+
},
309+
},
310+
},
311+
},
312+
},
313+
},
314+
},
315+
});
316+
243317
const issues = {
244318
createComment(params?: RestEndpointMethodTypes["issues"]["createComment"]["parameters"]) {
245319
return params;
@@ -254,9 +328,6 @@ describe("Event related tests", () => {
254328
octokit: {
255329
rest: {
256330
issues,
257-
repos: {
258-
getContent: jest.fn(getContent),
259-
},
260331
},
261332
},
262333
openAi: {
@@ -277,6 +348,7 @@ describe("Event related tests", () => {
277348
},
278349
},
279350
eventHandler: eventHandler,
351+
configurationHandler,
280352
payload: {
281353
...payload,
282354
comment: {
@@ -295,27 +367,17 @@ describe("Event related tests", () => {
295367
},
296368
};
297369
const spy = jest.spyOn(issues, "createComment");
298-
const getContent = jest.fn((params?: RestEndpointMethodTypes["repos"]["getContent"]["parameters"]) => {
299-
if (params?.path === CONFIG_FULL_PATH) {
300-
return {
301-
data: `
302-
plugins:
303-
ubiquity-os/plugin-b: {}
304-
`,
305-
};
306-
} else if (params?.path === "manifest.json") {
307-
return {
308-
data: {
309-
content: btoa(
310-
JSON.stringify({
311-
name: "plugin",
312-
})
313-
),
314-
},
315-
};
316-
} else {
317-
throw new Error("Not found");
318-
}
370+
const configurationHandler = createConfigurationHandler({
371+
configuration: {
372+
plugins: {
373+
"ubiquity-os/plugin-b": { with: {} },
374+
},
375+
},
376+
manifests: {
377+
"plugin-b": {
378+
name: "plugin",
379+
},
380+
},
319381
});
320382
const issueCommentCreated = (await import("../src/github/handlers/issue-comment-created")).default;
321383
await issueCommentCreated({
@@ -324,12 +386,10 @@ describe("Event related tests", () => {
324386
octokit: {
325387
rest: {
326388
issues,
327-
repos: {
328-
getContent: getContent,
329-
},
330389
},
331390
},
332391
eventHandler: eventHandler,
392+
configurationHandler,
333393
payload: {
334394
...payload,
335395
comment: {

0 commit comments

Comments
 (0)