@restranger/repi-test-utils 0.0.0-stage → 0.0.1-ca9b82c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +29 -5
- package/src/index.ts +20 -0
- package/src/mock-context.ts +207 -0
- package/src/mock-pi.ts +208 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@restranger/repi-test-utils",
|
|
3
|
+
"version": "0.0.1-ca9b82c",
|
|
4
|
+
"description": "Test mocks and utilities for testing Pi coding agent extensions and plugins",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"types": "./src/index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.ts"
|
|
10
|
+
},
|
|
11
|
+
"files": ["src", "README.md", "!src/**/__tests__"],
|
|
12
|
+
"keywords": ["pi-package", "pi-coding-agent", "testing", "mocks"],
|
|
13
|
+
"author": "ReStranger <restranger@disroot.org>",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/ReStranger/repi-mono.git",
|
|
18
|
+
"directory": "packages/test-utils"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/ReStranger/repi-mono/issues"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/ReStranger/repi-mono#readme",
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@restranger/repi-ui": "0.0.1-ca9b82c"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createMockPi,
|
|
3
|
+
type MockPi,
|
|
4
|
+
type MockPiCaptured,
|
|
5
|
+
type MockHandler,
|
|
6
|
+
} from "./mock-pi.js";
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
createMockExtensionContext,
|
|
10
|
+
type MockExtensionContext,
|
|
11
|
+
type MockExtensionUIContext,
|
|
12
|
+
type MockUICaptured,
|
|
13
|
+
type CannedResponses,
|
|
14
|
+
type MockExtensionContextOverrides,
|
|
15
|
+
} from "./mock-context.js";
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
renderToText,
|
|
19
|
+
type RenderToTextOptions,
|
|
20
|
+
} from "@restranger/repi-ui";
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
export interface MockUICaptured {
|
|
2
|
+
notifications: Array<{ message: string; type?: "info" | "warning" | "error" }>;
|
|
3
|
+
statuses: Map<string, string | undefined>;
|
|
4
|
+
widgets: Map<string, { content: any; options?: any }>;
|
|
5
|
+
selectCalls: Array<{ title: string; options: string[]; result?: string; opts?: unknown }>;
|
|
6
|
+
confirmCalls: Array<{ title: string; message: string; result: boolean; opts?: unknown }>;
|
|
7
|
+
inputCalls: Array<{ title: string; placeholder?: string; result?: string; opts?: unknown }>;
|
|
8
|
+
titles: string[];
|
|
9
|
+
workingMessages: string[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface CannedResponses {
|
|
13
|
+
select?: string | ((title: string, options: string[]) => string | undefined | Promise<string | undefined>);
|
|
14
|
+
confirm?: boolean | ((title: string, message: string) => boolean | Promise<boolean>);
|
|
15
|
+
input?: string | ((title: string, placeholder?: string) => string | undefined | Promise<string | undefined>);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface MockExtensionUIContext {
|
|
19
|
+
select(title: string, options: string[], opts?: unknown): Promise<string | undefined>;
|
|
20
|
+
confirm(title: string, message: string, opts?: unknown): Promise<boolean>;
|
|
21
|
+
input(title: string, placeholder?: string, opts?: unknown): Promise<string | undefined>;
|
|
22
|
+
notify(message: string, type?: "info" | "warning" | "error"): void;
|
|
23
|
+
setStatus(key: string, text: string | undefined): void;
|
|
24
|
+
setWidget(key: string, content: any, options?: any): void;
|
|
25
|
+
setTitle(title: string): void;
|
|
26
|
+
setWorkingMessage(message?: string): void;
|
|
27
|
+
editor(title: string, prefill?: string): Promise<string | undefined>;
|
|
28
|
+
custom<T>(factory: (...args: any[]) => any, options?: unknown): Promise<T>;
|
|
29
|
+
captured: MockUICaptured;
|
|
30
|
+
canned: CannedResponses;
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface MockExtensionContext {
|
|
35
|
+
ui: MockExtensionUIContext;
|
|
36
|
+
cwd: string;
|
|
37
|
+
sessionId: string;
|
|
38
|
+
sessionManager: { getSessionId(): string };
|
|
39
|
+
model: { id: string; provider: string; [key: string]: any };
|
|
40
|
+
mode: "tui" | "rpc" | "json" | "print";
|
|
41
|
+
hasUI: boolean;
|
|
42
|
+
captured: MockUICaptured;
|
|
43
|
+
canned: CannedResponses;
|
|
44
|
+
isIdle(): boolean;
|
|
45
|
+
isProjectTrusted(): boolean;
|
|
46
|
+
signal?: AbortSignal;
|
|
47
|
+
abort(): void;
|
|
48
|
+
hasPendingMessages(): boolean;
|
|
49
|
+
shutdown(): void;
|
|
50
|
+
getContextUsage(): unknown;
|
|
51
|
+
compact(options?: unknown): void;
|
|
52
|
+
getSystemPrompt(): string;
|
|
53
|
+
[key: string]: any;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface MockExtensionContextOverrides {
|
|
57
|
+
cwd?: string;
|
|
58
|
+
sessionId?: string;
|
|
59
|
+
sessionManager?: { getSessionId(): string; [key: string]: any };
|
|
60
|
+
model?: { id: string; provider: string; [key: string]: any };
|
|
61
|
+
mode?: "tui" | "rpc" | "json" | "print";
|
|
62
|
+
hasUI?: boolean;
|
|
63
|
+
selectResult?: string | ((title: string, options: string[]) => string | undefined | Promise<string | undefined>);
|
|
64
|
+
confirmResult?: boolean | ((title: string, message: string) => boolean | Promise<boolean>);
|
|
65
|
+
inputResult?: string | ((title: string, placeholder?: string) => string | undefined | Promise<string | undefined>);
|
|
66
|
+
canned?: CannedResponses;
|
|
67
|
+
signal?: AbortSignal;
|
|
68
|
+
[key: string]: any;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function createMockExtensionContext(overrides?: MockExtensionContextOverrides): MockExtensionContext {
|
|
72
|
+
const notifications: Array<{ message: string; type?: "info" | "warning" | "error" }> = [];
|
|
73
|
+
const statuses = new Map<string, string | undefined>();
|
|
74
|
+
const widgets = new Map<string, { content: any; options?: any }>();
|
|
75
|
+
const selectCalls: Array<{ title: string; options: string[]; result?: string; opts?: unknown }> = [];
|
|
76
|
+
const confirmCalls: Array<{ title: string; message: string; result: boolean; opts?: unknown }> = [];
|
|
77
|
+
const inputCalls: Array<{ title: string; placeholder?: string; result?: string; opts?: unknown }> = [];
|
|
78
|
+
const titles: string[] = [];
|
|
79
|
+
const workingMessages: string[] = [];
|
|
80
|
+
|
|
81
|
+
const captured: MockUICaptured = {
|
|
82
|
+
notifications,
|
|
83
|
+
statuses,
|
|
84
|
+
widgets,
|
|
85
|
+
selectCalls,
|
|
86
|
+
confirmCalls,
|
|
87
|
+
inputCalls,
|
|
88
|
+
titles,
|
|
89
|
+
workingMessages,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const canned: CannedResponses = {
|
|
93
|
+
select: overrides?.selectResult ?? overrides?.canned?.select,
|
|
94
|
+
confirm: overrides?.confirmResult ?? overrides?.canned?.confirm,
|
|
95
|
+
input: overrides?.inputResult ?? overrides?.canned?.input,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const ui: MockExtensionUIContext = {
|
|
99
|
+
captured,
|
|
100
|
+
canned,
|
|
101
|
+
async select(title: string, options: string[], opts?: unknown): Promise<string | undefined> {
|
|
102
|
+
let res: string | undefined;
|
|
103
|
+
const handler = canned.select;
|
|
104
|
+
if (typeof handler === "function") {
|
|
105
|
+
res = await handler(title, options);
|
|
106
|
+
} else if (handler !== undefined) {
|
|
107
|
+
res = handler;
|
|
108
|
+
} else {
|
|
109
|
+
// Fail closed: a mock must not pick a choice the test did not specify.
|
|
110
|
+
res = undefined;
|
|
111
|
+
}
|
|
112
|
+
selectCalls.push({ title, options, result: res, opts });
|
|
113
|
+
return res;
|
|
114
|
+
},
|
|
115
|
+
async confirm(title: string, message: string, opts?: unknown): Promise<boolean> {
|
|
116
|
+
let res: boolean;
|
|
117
|
+
const handler = canned.confirm;
|
|
118
|
+
if (typeof handler === "function") {
|
|
119
|
+
res = await handler(title, message);
|
|
120
|
+
} else if (typeof handler === "boolean") {
|
|
121
|
+
res = handler;
|
|
122
|
+
} else {
|
|
123
|
+
// Fail closed: a mock must not approve a dialog the test did not answer.
|
|
124
|
+
res = false;
|
|
125
|
+
}
|
|
126
|
+
confirmCalls.push({ title, message, result: res, opts });
|
|
127
|
+
return res;
|
|
128
|
+
},
|
|
129
|
+
async input(title: string, placeholder?: string, opts?: unknown): Promise<string | undefined> {
|
|
130
|
+
let res: string | undefined;
|
|
131
|
+
const handler = canned.input;
|
|
132
|
+
if (typeof handler === "function") {
|
|
133
|
+
res = await handler(title, placeholder);
|
|
134
|
+
} else if (handler !== undefined) {
|
|
135
|
+
res = handler;
|
|
136
|
+
} else {
|
|
137
|
+
res = placeholder ?? "";
|
|
138
|
+
}
|
|
139
|
+
inputCalls.push({ title, placeholder, result: res, opts });
|
|
140
|
+
return res;
|
|
141
|
+
},
|
|
142
|
+
notify(message: string, type?: "info" | "warning" | "error") {
|
|
143
|
+
notifications.push({ message, type });
|
|
144
|
+
},
|
|
145
|
+
setStatus(key: string, text: string | undefined) {
|
|
146
|
+
statuses.set(key, text);
|
|
147
|
+
},
|
|
148
|
+
setWidget(key: string, content: any, options?: any) {
|
|
149
|
+
widgets.set(key, { content, options });
|
|
150
|
+
},
|
|
151
|
+
setTitle(title: string) {
|
|
152
|
+
titles.push(title);
|
|
153
|
+
},
|
|
154
|
+
setWorkingMessage(message?: string) {
|
|
155
|
+
workingMessages.push(message ?? "");
|
|
156
|
+
},
|
|
157
|
+
async editor(_title: string, prefill?: string): Promise<string | undefined> {
|
|
158
|
+
return prefill ?? "";
|
|
159
|
+
},
|
|
160
|
+
async custom<T>(_factory: (...args: any[]) => any, _options?: unknown): Promise<T> {
|
|
161
|
+
return undefined as unknown as T;
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const ctx: MockExtensionContext = {
|
|
166
|
+
ui,
|
|
167
|
+
captured,
|
|
168
|
+
canned,
|
|
169
|
+
cwd: overrides?.cwd ?? process.cwd(),
|
|
170
|
+
sessionId: overrides?.sessionId ?? "test-session-id",
|
|
171
|
+
sessionManager: overrides?.sessionManager ?? {
|
|
172
|
+
getSessionId() {
|
|
173
|
+
return ctx.sessionId;
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
model: overrides?.model ?? { id: "test-model", provider: "test" },
|
|
177
|
+
mode: overrides?.mode ?? "tui",
|
|
178
|
+
hasUI: overrides?.hasUI ?? true,
|
|
179
|
+
signal: overrides?.signal,
|
|
180
|
+
isIdle() {
|
|
181
|
+
return true;
|
|
182
|
+
},
|
|
183
|
+
isProjectTrusted() {
|
|
184
|
+
return true;
|
|
185
|
+
},
|
|
186
|
+
abort() {},
|
|
187
|
+
hasPendingMessages() {
|
|
188
|
+
return false;
|
|
189
|
+
},
|
|
190
|
+
shutdown() {},
|
|
191
|
+
getContextUsage() {
|
|
192
|
+
return undefined;
|
|
193
|
+
},
|
|
194
|
+
compact(_options?: unknown) {},
|
|
195
|
+
getSystemPrompt() {
|
|
196
|
+
return "";
|
|
197
|
+
},
|
|
198
|
+
...overrides,
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// Ensure ui is not overwritten improperly if overrides had other fields
|
|
202
|
+
ctx.ui = ui;
|
|
203
|
+
ctx.captured = captured;
|
|
204
|
+
ctx.canned = canned;
|
|
205
|
+
|
|
206
|
+
return ctx;
|
|
207
|
+
}
|
package/src/mock-pi.ts
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
export type MockHandler = (...args: any[]) => any;
|
|
2
|
+
|
|
3
|
+
export interface MockToolSpec {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly description?: string;
|
|
6
|
+
readonly parameters?: unknown;
|
|
7
|
+
readonly execute?: (...args: unknown[]) => unknown;
|
|
8
|
+
readonly renderCall?: (...args: any[]) => any;
|
|
9
|
+
readonly renderResult?: (...args: any[]) => any;
|
|
10
|
+
readonly [key: string]: unknown;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface MockPiCaptured {
|
|
14
|
+
readonly tools: Map<string, MockToolSpec>;
|
|
15
|
+
readonly commands: Map<string, unknown>;
|
|
16
|
+
readonly shortcuts: Map<string, unknown>;
|
|
17
|
+
readonly flags: Map<string, unknown>;
|
|
18
|
+
readonly flagValues: Map<string, unknown>;
|
|
19
|
+
readonly hooks: Map<string, MockHandler[]>;
|
|
20
|
+
readonly messages: unknown[];
|
|
21
|
+
readonly userMessages: Array<{ content: unknown; options?: unknown }>;
|
|
22
|
+
readonly entries: Array<{ type: string; data?: unknown }>;
|
|
23
|
+
readonly sessionNames: string[];
|
|
24
|
+
readonly labels: Map<string, string | undefined>;
|
|
25
|
+
readonly selectedModels: unknown[];
|
|
26
|
+
fireHook(event: string, payload?: unknown, ctx?: unknown): Promise<unknown[]>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface MockPi {
|
|
30
|
+
readonly captured: MockPiCaptured;
|
|
31
|
+
registerTool(toolSpec: MockToolSpec): void;
|
|
32
|
+
registerCommand(name: string, options: unknown): void;
|
|
33
|
+
setModel(model: unknown): Promise<boolean>;
|
|
34
|
+
registerShortcut(key: string, options: unknown): void;
|
|
35
|
+
registerFlag(name: string, options: unknown): void;
|
|
36
|
+
getFlag(name: string): unknown;
|
|
37
|
+
setFlag(name: string, value: unknown): void;
|
|
38
|
+
on(event: string, handler: MockHandler): () => void;
|
|
39
|
+
sendMessage(msg: unknown, options?: unknown): void;
|
|
40
|
+
sendUserMessage(content: unknown, options?: unknown): void;
|
|
41
|
+
appendEntry(type: string, data?: unknown): void;
|
|
42
|
+
fireHook(event: string, payload?: unknown, ctx?: unknown): Promise<unknown[]>;
|
|
43
|
+
setSessionName(name: string): void;
|
|
44
|
+
getSessionName(): string | undefined;
|
|
45
|
+
setLabel(entryId: string, label: string | undefined): void;
|
|
46
|
+
/** Tools the model can currently see (reflects `setActiveTools`). */
|
|
47
|
+
getActiveTools(): string[];
|
|
48
|
+
getAllTools(): MockToolSpec[];
|
|
49
|
+
setActiveTools(toolNames: string[]): void;
|
|
50
|
+
getCommands(): Array<{ name: string } & Record<string, unknown>>;
|
|
51
|
+
getSettings(): Record<string, unknown>;
|
|
52
|
+
readonly events: {
|
|
53
|
+
on(event: string, handler: MockHandler): () => void;
|
|
54
|
+
emit(event: string, data?: unknown): void;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function createMockPi(): MockPi {
|
|
59
|
+
const tools = new Map<string, MockToolSpec>();
|
|
60
|
+
const commands = new Map<string, unknown>();
|
|
61
|
+
const shortcuts = new Map<string, unknown>();
|
|
62
|
+
const flags = new Map<string, unknown>();
|
|
63
|
+
const flagValues = new Map<string, unknown>();
|
|
64
|
+
const hooks = new Map<string, MockHandler[]>();
|
|
65
|
+
const messages: unknown[] = [];
|
|
66
|
+
const userMessages: Array<{ content: unknown; options?: unknown }> = [];
|
|
67
|
+
const entries: Array<{ type: string; data?: unknown }> = [];
|
|
68
|
+
const sessionNames: string[] = [];
|
|
69
|
+
const labels = new Map<string, string | undefined>();
|
|
70
|
+
const selectedModels: unknown[] = [];
|
|
71
|
+
const eventBusListeners = new Map<string, MockHandler[]>();
|
|
72
|
+
let activeTools: string[] | undefined;
|
|
73
|
+
|
|
74
|
+
const fireHook = async (event: string, payload?: unknown, ctx?: unknown): Promise<unknown[]> => {
|
|
75
|
+
const handlers = hooks.get(event) ?? [];
|
|
76
|
+
const results: unknown[] = [];
|
|
77
|
+
for (const handler of handlers) {
|
|
78
|
+
results.push(await handler(payload, ctx));
|
|
79
|
+
}
|
|
80
|
+
return results;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const captured: MockPiCaptured = {
|
|
84
|
+
tools,
|
|
85
|
+
commands,
|
|
86
|
+
shortcuts,
|
|
87
|
+
flags,
|
|
88
|
+
flagValues,
|
|
89
|
+
hooks,
|
|
90
|
+
messages,
|
|
91
|
+
userMessages,
|
|
92
|
+
entries,
|
|
93
|
+
sessionNames,
|
|
94
|
+
labels,
|
|
95
|
+
selectedModels,
|
|
96
|
+
fireHook,
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const pi: MockPi = {
|
|
100
|
+
captured,
|
|
101
|
+
registerTool(toolSpec: MockToolSpec) {
|
|
102
|
+
tools.set(toolSpec.name, toolSpec);
|
|
103
|
+
},
|
|
104
|
+
registerCommand(name: string, options: unknown) {
|
|
105
|
+
commands.set(name, options);
|
|
106
|
+
},
|
|
107
|
+
setModel(model: unknown) {
|
|
108
|
+
selectedModels.push(model);
|
|
109
|
+
return Promise.resolve(true);
|
|
110
|
+
},
|
|
111
|
+
registerShortcut(key: string, options: unknown) {
|
|
112
|
+
shortcuts.set(key, options);
|
|
113
|
+
},
|
|
114
|
+
registerFlag(name: string, options: unknown) {
|
|
115
|
+
flags.set(name, options);
|
|
116
|
+
const defaults = options as { default?: unknown } | undefined;
|
|
117
|
+
if (defaults?.default !== undefined && !flagValues.has(name)) {
|
|
118
|
+
flagValues.set(name, defaults.default);
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
getFlag(name: string) {
|
|
122
|
+
return flagValues.get(name) ?? (flags.get(name) as { default?: unknown } | undefined)?.default;
|
|
123
|
+
},
|
|
124
|
+
setFlag(name: string, value: unknown) {
|
|
125
|
+
flagValues.set(name, value);
|
|
126
|
+
},
|
|
127
|
+
on(event: string, handler: MockHandler) {
|
|
128
|
+
let list = hooks.get(event);
|
|
129
|
+
if (!list) {
|
|
130
|
+
list = [];
|
|
131
|
+
hooks.set(event, list);
|
|
132
|
+
}
|
|
133
|
+
list.push(handler);
|
|
134
|
+
return () => {
|
|
135
|
+
const current = hooks.get(event);
|
|
136
|
+
if (!current) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const index = current.indexOf(handler);
|
|
140
|
+
if (index !== -1) {
|
|
141
|
+
current.splice(index, 1);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
},
|
|
145
|
+
sendMessage(msg: unknown, options?: unknown) {
|
|
146
|
+
messages.push(options === undefined ? msg : { msg, options });
|
|
147
|
+
},
|
|
148
|
+
sendUserMessage(content: unknown, options?: unknown) {
|
|
149
|
+
userMessages.push({ content, options });
|
|
150
|
+
messages.push({ role: "user", content, options });
|
|
151
|
+
},
|
|
152
|
+
appendEntry(type: string, data?: unknown) {
|
|
153
|
+
entries.push({ type, data });
|
|
154
|
+
},
|
|
155
|
+
fireHook,
|
|
156
|
+
setSessionName(name: string) {
|
|
157
|
+
sessionNames.push(name);
|
|
158
|
+
},
|
|
159
|
+
getSessionName() {
|
|
160
|
+
return sessionNames.length > 0 ? sessionNames[sessionNames.length - 1] : undefined;
|
|
161
|
+
},
|
|
162
|
+
setLabel(entryId: string, label: string | undefined) {
|
|
163
|
+
labels.set(entryId, label);
|
|
164
|
+
},
|
|
165
|
+
getActiveTools() {
|
|
166
|
+
return activeTools ? [...activeTools] : [...tools.keys()];
|
|
167
|
+
},
|
|
168
|
+
getAllTools() {
|
|
169
|
+
return [...tools.values()];
|
|
170
|
+
},
|
|
171
|
+
setActiveTools(toolNames: string[]) {
|
|
172
|
+
activeTools = [...toolNames];
|
|
173
|
+
},
|
|
174
|
+
getCommands() {
|
|
175
|
+
return [...commands.entries()].map(([name, spec]) => ({ name, ...(spec as Record<string, unknown>) }));
|
|
176
|
+
},
|
|
177
|
+
getSettings() {
|
|
178
|
+
return {};
|
|
179
|
+
},
|
|
180
|
+
events: {
|
|
181
|
+
on(event: string, handler: MockHandler) {
|
|
182
|
+
let list = eventBusListeners.get(event);
|
|
183
|
+
if (!list) {
|
|
184
|
+
list = [];
|
|
185
|
+
eventBusListeners.set(event, list);
|
|
186
|
+
}
|
|
187
|
+
list.push(handler);
|
|
188
|
+
return () => {
|
|
189
|
+
const current = eventBusListeners.get(event);
|
|
190
|
+
if (!current) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const index = current.indexOf(handler);
|
|
194
|
+
if (index !== -1) {
|
|
195
|
+
current.splice(index, 1);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
emit(event: string, data?: unknown) {
|
|
200
|
+
for (const handler of eventBusListeners.get(event) ?? []) {
|
|
201
|
+
handler(data);
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
return pi;
|
|
208
|
+
}
|
package/README.md
DELETED