-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathmock-gh-api-server.ts
More file actions
151 lines (119 loc) · 3.59 KB
/
mock-gh-api-server.ts
File metadata and controls
151 lines (119 loc) · 3.59 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
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
import { join, resolve } from "path";
import { pathExists } from "fs-extra";
import type { SetupServer } from "msw/node";
import { setupServer } from "msw/node";
import type { UnhandledRequestStrategy } from "msw/lib/core/utils/request/onUnhandledRequest";
import { DisposableObject } from "../disposable-object";
import { Recorder } from "./recorder";
import { createRequestHandlers } from "./request-handlers";
import { getDirectoryNamesInsidePath } from "../files";
/**
* Enables mocking of the GitHub API server via HTTP interception, using msw.
*/
export class MockGitHubApiServer extends DisposableObject {
private _isListening: boolean;
private readonly server: SetupServer;
private readonly recorder: Recorder;
constructor() {
super();
this._isListening = false;
this.server = setupServer();
this.recorder = this.push(new Recorder(this.server));
}
public startServer(
onUnhandledRequest: UnhandledRequestStrategy = "bypass",
): void {
if (this._isListening) {
return;
}
this.server.listen({ onUnhandledRequest });
this._isListening = true;
}
public stopServer(): void {
this.server.close();
this._isListening = false;
}
public async loadScenario(
scenarioName: string,
scenariosPath?: string,
): Promise<void> {
if (!scenariosPath) {
scenariosPath = await this.getDefaultScenariosPath();
if (!scenariosPath) {
return;
}
}
const scenarioPath = join(scenariosPath, scenarioName);
const handlers = await createRequestHandlers(scenarioPath);
this.server.resetHandlers(...handlers);
}
public async saveScenario(
scenarioName: string,
scenariosPath?: string,
): Promise<string> {
if (!scenariosPath) {
scenariosPath = await this.getDefaultScenariosPath();
if (!scenariosPath) {
throw new Error("Could not find scenarios path");
}
}
const filePath = await this.recorder.save(scenariosPath, scenarioName);
await this.stopRecording();
return filePath;
}
public async unloadScenario(): Promise<void> {
if (!this.isScenarioLoaded) {
return;
}
await this.unloadAllScenarios();
}
public async startRecording(): Promise<void> {
if (this.recorder.isRecording) {
return;
}
if (this.isScenarioLoaded) {
await this.unloadAllScenarios();
}
this.recorder.start();
}
public async stopRecording(): Promise<void> {
this.recorder.stop();
this.recorder.clear();
}
public async getScenarioNames(scenariosPath?: string): Promise<string[]> {
if (!scenariosPath) {
scenariosPath = await this.getDefaultScenariosPath();
if (!scenariosPath) {
return [];
}
}
return await getDirectoryNamesInsidePath(scenariosPath);
}
public get isListening(): boolean {
return this._isListening;
}
public get isRecording(): boolean {
return this.recorder.isRecording;
}
public get anyRequestsRecorded(): boolean {
return this.recorder.anyRequestsRecorded;
}
public get isScenarioLoaded(): boolean {
return this.server.listHandlers().length > 0;
}
public async getDefaultScenariosPath(): Promise<string | undefined> {
// This should be the directory where package.json is located
const rootDirectory = resolve(__dirname, "../../..");
const scenariosPath = resolve(
rootDirectory,
"src/common/mock-gh-api/scenarios",
);
if (await pathExists(scenariosPath)) {
return scenariosPath;
}
return undefined;
}
private async unloadAllScenarios(): Promise<void> {
this.server.resetHandlers();
}
}