Skip to content

Commit d283f7e

Browse files
authored
chore(tests): swap to @simulacrum/github-api-simulator (#330)
1 parent d904df2 commit d283f7e

10 files changed

+2060
-910
lines changed

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"devDependencies": {
2525
"@jsdevtools/ez-spawn": "^3.0.4",
2626
"@types/node": "^20.14.2",
27+
"@simulacrum/github-api-simulator": "^0.5.3",
2728
"@vitejs/release-scripts": "^1.3.1",
2829
"cross-env": "^7.0.3",
2930
"esbuild": "^0.20.2",

Diff for: packages/backend/e2e.test.ts

+21-29
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,43 @@ import ezSpawn from "@jsdevtools/ez-spawn"
55
import pushWorkflowRunInProgressFixture from './fixtures/workflow_run.in_progress.json'
66
import prWorkflowRunRequestedFixture from './fixtures/pr.workflow_run.requested.json'
77
import prPullRequestSynchronizeFixture from './fixtures/pr.pull_request.json'
8-
import { setupServer } from 'msw/node'
9-
import { http, HttpResponse } from 'msw'
10-
11-
const server = setupServer(
12-
// Mock the GitHub API installation endpoint
13-
http.get('https://api.github.com/repos/:owner/:repo/installation', (ctx) => {
14-
return HttpResponse.json({
15-
id: 1234567,
16-
access_tokens_url: 'https://api.github.com/app/installations/1234567/access_tokens',
17-
})
18-
}),
8+
import { simulation } from '@simulacrum/github-api-simulator'
199

20-
// Mock the GitHub API token endpoint
21-
http.post('https://api.github.com/app/installations/:installation_id/access_tokens', (ctx) => {
22-
return HttpResponse.json({
23-
token: 'ghs_mock_token',
24-
expires_at: '2024-03-22T00:00:00Z',
25-
})
26-
})
27-
)
10+
let server;
11+
let workerUrl: string;
2812

2913
let worker: UnstableDevWorker
30-
3114
beforeAll(async () => {
32-
// Start MSW server
33-
server.listen()
15+
const app = simulation({
16+
initialState: {
17+
users: [],
18+
organizations: [{ login: "stackblitz-labs" }],
19+
repositories: [{ owner: "stackblitz-labs", name: "temporary-test" }],
20+
branches: [{ name: "main" }],
21+
blobs: [],
22+
},
23+
});
24+
server = await app.listen(3300);
3425

3526
await ezSpawn.async('pnpm cross-env TEST=true pnpm --filter=backend run build', [], {
3627
stdio: "inherit",
3728
shell: true,
3829
});
39-
worker = await unstable_dev('dist/_worker.js', {
40-
config: './wrangler.toml',
30+
worker = await unstable_dev(`${import.meta.dirname}/dist/_worker.js`, {
31+
config: `${import.meta.dirname}/wrangler.toml`,
4132
})
4233
const url = `${worker.proxyData.userWorkerUrl.protocol}//${worker.proxyData.userWorkerUrl.hostname}:${worker.proxyData.userWorkerUrl.port}`
4334
console.log(url)
35+
workerUrl = url
4436
await ezSpawn.async(`pnpm cross-env TEST=true API_URL=${url} pnpm --filter=pkg-pr-new run build`, [], {
4537
stdio: "inherit",
4638
shell: true,
4739
});
4840
})
4941

50-
afterAll(() => {
51-
server.close()
52-
})
42+
afterAll(async () => {
43+
await server.ensureClose();
44+
});
5345

5446
describe.sequential.each([
5547
[pushWorkflowRunInProgressFixture],
@@ -135,7 +127,7 @@ describe.sequential.each([
135127
expect(shaBlobSize).toEqual(refBlobSize);
136128

137129
// Test installation
138-
const url = `/${owner}/${repo}/playground-a@${sha}?id=${Date.now()}`
130+
const url = new URL(`/${owner}/${repo}/playground-a@${sha}?id=${Date.now()}`,workerUrl)
139131
const installProcess = await ezSpawn.async(`pnpm cross-env CI=true npx -f playground-a@${url}`, {
140132
stdio: "overlapped",
141133
shell: true,
@@ -152,7 +144,7 @@ describe.sequential.each([
152144
expect(response.status).toBe(200)
153145

154146
// Test installation
155-
const url = `/${owner}/${repo}/playground-b@${sha}?id=${Date.now()}`
147+
const url = new URL(`/${owner}/${repo}/playground-b@${sha}?id=${Date.now()}`,workerUrl)
156148
const installProcess = await ezSpawn.async(`pnpm cross-env CI=true npx -f playground-b@${url}`, {
157149
stdio: "overlapped",
158150
shell: true,

Diff for: packages/backend/fixtures/pr.pull_request.json

+222-222
Large diffs are not rendered by default.

Diff for: packages/backend/fixtures/pr.workflow_run.completed.json

+201-201
Large diffs are not rendered by default.

Diff for: packages/backend/fixtures/pr.workflow_run.requested.json

+201-201
Large diffs are not rendered by default.

Diff for: packages/backend/fixtures/workflow_run.in_progress.json

+202-202
Large diffs are not rendered by default.

Diff for: packages/backend/nitro.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default defineNitroConfig({
2727
srcDir: "server",
2828
runtimeConfig: {
2929
appId: "",
30+
ghBaseUrl: "https://api.github.com",
3031
webhookSecret: "",
3132
privateKey: "",
3233
rmStaleKey: "",

Diff for: packages/backend/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
"@octokit/webhooks": "^12.2.0",
2626
"@octokit/webhooks-types": "^7.4.0",
2727
"@pkg-pr-new/utils": "workspace:^",
28+
"@simulacrum/github-api-simulator": "^0.5.3",
2829
"dotenv": "^16.4.5",
2930
"fast-jwt": "^4.0.2",
3031
"h3": "^1.11.1",
31-
"msw": "^2.7.0",
3232
"nitro-cloudflare-dev": "0.1.2",
3333
"nitropack": "^2.9.6",
3434
"octokit": "^3.2.1",

Diff for: packages/backend/server/utils/octokit.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import type { H3Event } from "h3";
22
import type { App as AppType } from "octokit";
3-
import { App } from "../../vendor/octokit.build.mjs";
3+
import { App, Octokit } from "../../vendor/octokit.build.mjs";
44

55
export function useOctokitApp(event: H3Event): AppType {
6-
const { appId, privateKey, webhookSecret } = useRuntimeConfig(event);
6+
const { appId, privateKey, webhookSecret, ghBaseUrl } =
7+
useRuntimeConfig(event);
78

89
return new App({
910
appId,
1011
privateKey,
1112
webhooks: { secret: webhookSecret },
13+
Octokit: Octokit.defaults({
14+
baseUrl: ghBaseUrl,
15+
}),
1216
}) as unknown as AppType;
1317
}
1418

@@ -20,10 +24,7 @@ export async function useOctokitInstallation(
2024
const app = useOctokitApp(event);
2125
const { data: installationData } = await app.octokit.request(
2226
"GET /repos/{owner}/{repo}/installation",
23-
{
24-
owner: owner,
25-
repo: repo,
26-
},
27+
{ owner, repo },
2728
);
2829

2930
return app.getInstallationOctokit(installationData.id);

0 commit comments

Comments
 (0)