From 43d76ab6e4a9790bd2f86fd516814b0b9c0ee1b8 Mon Sep 17 00:00:00 2001 From: Aaron Cox Date: Thu, 12 Oct 2023 15:50:59 -0700 Subject: [PATCH] Adding missing/unused utils --- test/utils/mock-fetch.ts | 64 +++++++++++++++++++++++++ test/utils/mock-storage.ts | 17 +++++++ test/utils/mock-ui.ts | 98 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 test/utils/mock-fetch.ts create mode 100644 test/utils/mock-storage.ts create mode 100644 test/utils/mock-ui.ts diff --git a/test/utils/mock-fetch.ts b/test/utils/mock-fetch.ts new file mode 100644 index 0000000..272f6fb --- /dev/null +++ b/test/utils/mock-fetch.ts @@ -0,0 +1,64 @@ +import fetch, { Response } from 'node-fetch' +import { join as joinPath } from 'path' +import { promisify } from 'util' +import { readFile as _readFile, writeFile as _writeFile } from 'fs' +import { Bytes, Checksum160 } from '@wharfkit/session' + +const readFile = promisify(_readFile) +const writeFile = promisify(_writeFile) + +function getFilename(path: string, params?: unknown) { + const digest = Checksum160.hash( + Bytes.from(path + (params ? JSON.stringify(params) : ''), 'utf8') + ).hexString + return joinPath(__dirname, '../data', digest + '.json') +} + +async function getExisting(filename: string) { + try { + const data = await readFile(filename) + return JSON.parse(data.toString('utf8')) + } catch (error) { + if ((error).code !== 'ENOENT') { + throw error + } + } +} + +export async function mockFetch(path, params) { + const filename = getFilename(path, params) + if (process.env['MOCK'] !== 'overwrite') { + const existing = await getExisting(filename) + if (existing) { + return new Response(existing.text, { + status: existing.status, + headers: existing.headers, + }) + } + } + if (process.env['MOCK']) { + const response = await fetch(path, params) + const cloned = response.clone() + const json = await cloned.json() + await writeFile( + filename, + JSON.stringify( + { + request: { + path, + params, + }, + headers: Object.fromEntries(response.headers.entries()), + status: response.status, + json, + text: JSON.stringify(json), + }, + undefined, + 4 + ) + ) + return response + } else { + throw new Error(`No data for ${path}`) + } +} diff --git a/test/utils/mock-storage.ts b/test/utils/mock-storage.ts new file mode 100644 index 0000000..af13dbf --- /dev/null +++ b/test/utils/mock-storage.ts @@ -0,0 +1,17 @@ +import { SessionStorage } from '@wharfkit/session' + +export class MockStorage implements SessionStorage { + data: Record = {} + async write(key: string, data: string): Promise { + this.data[key] = data + } + async read(key: string): Promise { + return this.data[key] + } + async remove(key: string): Promise { + delete this.data[key] + } + storageKey(key: string) { + return `mock-${key}` + } +} diff --git a/test/utils/mock-ui.ts b/test/utils/mock-ui.ts new file mode 100644 index 0000000..2aba81d --- /dev/null +++ b/test/utils/mock-ui.ts @@ -0,0 +1,98 @@ +import { + AbstractUserInterface, + cancelable, + Cancelable, + Checksum256, + LoginContext, + LoginOptions, + PermissionLevel, + PromptArgs, + PromptResponse, + UserInterface, + UserInterfaceLoginResponse, +} from '@wharfkit/session' + +export class MockUserInterface extends AbstractUserInterface implements UserInterface { + readonly logging = false + public messages: string[] = [] + + translate(key) { + return key + } + + log(message: string) { + this.messages.push(message) + if (this.logging) { + // eslint-disable-next-line no-console + console.info('MockUserInterface', message) + } + } + + async login(context: LoginContext): Promise { + let chainId = context.chain?.id + if (!chainId) { + chainId = Checksum256.from(context.chains[0].id) + } + let permissionLevel = context.permissionLevel + if (!permissionLevel) { + permissionLevel = PermissionLevel.from('mock@interface') + } + return { + chainId, + permissionLevel, + walletPluginIndex: 0, + } + } + + async onError(error: Error) { + this.log('onError: ' + JSON.stringify(error)) + } + + async onLogin(options?: LoginOptions) { + this.log('onLogin: ' + JSON.stringify(options)) + } + + async onLoginComplete() { + this.log('onLoginComplete') + } + + async onTransact() { + this.log('onTransact') + } + + async onTransactComplete() { + this.log('onTransactComplete') + } + + async onSign() { + this.log('onSign') + } + + async onSignComplete() { + this.log('onSignComplete') + } + + async onBroadcast() { + this.log('onBroadcast') + } + + async onBroadcastComplete() { + this.log('onBroadcastComplete') + } + + prompt(args: PromptArgs): Cancelable { + this.log('prompt' + JSON.stringify(args)) + return cancelable(new Promise(() => {}), (canceled) => { + // do things to cancel promise + throw canceled + }) + } + + status(message: string) { + this.log(`status:('${message}')`) + } + + addTranslations(): void { + this.log('addTranslations') + } +}