(
overrides: RpcOverrides,
streamOverrides: RpcStreamOverrides,
wos: MockWosFns
)
| 206 | }; |
| 207 | |
| 208 | export function makeMockRpc( |
| 209 | overrides: RpcOverrides, |
| 210 | streamOverrides: RpcStreamOverrides, |
| 211 | wos: MockWosFns |
| 212 | ): { |
| 213 | rpc: RpcApiType; |
| 214 | setRpcHandler: (command: string, fn: RpcHandlerType) => void; |
| 215 | setRpcStreamHandler: (command: string, fn: RpcStreamHandlerType) => void; |
| 216 | } { |
| 217 | const callDispatchMap = new Map<string, (...args: any[]) => Promise<any>>(); |
| 218 | const streamDispatchMap = new Map<string, (...args: any[]) => AsyncGenerator<any, void, boolean>>(); |
| 219 | const secrets = new Map<string, string>(); |
| 220 | const setCallHandler = (command: string, fn: (...args: any[]) => Promise<any>) => { |
| 221 | callDispatchMap.set(command, fn); |
| 222 | }; |
| 223 | const setStreamHandler = (command: string, fn: (...args: any[]) => AsyncGenerator<any, void, boolean>) => { |
| 224 | streamDispatchMap.set(command, fn); |
| 225 | }; |
| 226 | setCallHandler("eventpublish", async (_client, data: WaveEvent) => { |
| 227 | console.log("[mock eventpublish]", data); |
| 228 | handleWaveEvent(data); |
| 229 | return null; |
| 230 | }); |
| 231 | setCallHandler("getmeta", async (_client, data: CommandGetMetaData) => { |
| 232 | const objAtom = wos.getWaveObjectAtom(data.oref); |
| 233 | const current = globalStore.get(objAtom) as WaveObj & { meta?: MetaType }; |
| 234 | return current?.meta ?? {}; |
| 235 | }); |
| 236 | setCallHandler("setmeta", async (_client, data: CommandSetMetaData) => { |
| 237 | const objAtom = wos.getWaveObjectAtom(data.oref); |
| 238 | const current = globalStore.get(objAtom) as WaveObj & { meta?: MetaType }; |
| 239 | const updatedMeta = { ...(current?.meta ?? {}) }; |
| 240 | for (const [key, value] of Object.entries(data.meta)) { |
| 241 | if (value === null) { |
| 242 | delete updatedMeta[key]; |
| 243 | } else { |
| 244 | (updatedMeta as any)[key] = value; |
| 245 | } |
| 246 | } |
| 247 | const updated = { ...current, meta: updatedMeta }; |
| 248 | wos.mockSetWaveObj(data.oref, updated); |
| 249 | return null; |
| 250 | }); |
| 251 | setCallHandler("updatetabname", async (_client, data: { args: [string, string] }) => { |
| 252 | const [tabId, newName] = data.args; |
| 253 | const tabORef = "tab:" + tabId; |
| 254 | const objAtom = wos.getWaveObjectAtom(tabORef); |
| 255 | const current = globalStore.get(objAtom) as Tab; |
| 256 | const updated = { ...current, name: newName }; |
| 257 | wos.mockSetWaveObj(tabORef, updated); |
| 258 | return null; |
| 259 | }); |
| 260 | setCallHandler("setconfig", async (_client, data: SettingsType) => { |
| 261 | const current = globalStore.get(wos.fullConfigAtom); |
| 262 | const updatedSettings = { ...(current?.settings ?? {}) }; |
| 263 | for (const [key, value] of Object.entries(data)) { |
| 264 | if (value === null) { |
| 265 | delete (updatedSettings as any)[key]; |
no test coverage detected