import {AbsURLScheme} from 'scriptable-abstract'; interface URLSchemeState { schemes: Set; lastOpenedURL: string | null; } const DEFAULT_STATE: URLSchemeState = { schemes: new Set(), lastOpenedURL: null, }; /** * Mock implementation of Scriptable's URLScheme global variable * @implements URLScheme */ export class MockURLScheme extends AbsURLScheme { static get instance(): MockURLScheme { return super.instance as MockURLScheme; } constructor() { super(DEFAULT_STATE); } /** * @inheritdoc */ async open(urlScheme: string): Promise { this.setState({lastOpenedURL: urlScheme}); } /** * @inheritdoc */ async fromURLString(urlString: string): Promise { return urlString; } /** * @inheritdoc */ async forActionExtension(): Promise { return this.state.lastOpenedURL ?? ''; } /** * @inheritdoc */ async forNotification(): Promise { return this.state.lastOpenedURL ?? ''; } /** * @additional * Register a URL scheme for testing */ registerScheme(scheme: string): void { this.setState(state => ({ schemes: new Set([...state.schemes, scheme]), })); } /** * @additional * Check if a URL scheme is registered */ isRegistered(scheme: string): boolean { return this.state.schemes.has(scheme); } /** * @additional * Get the last opened URL */ getLastOpenedURL(): string | null { return this.state.lastOpenedURL; } /** * @additional * Clear all registered schemes and last opened URL */ clear(): void { this.setState({ schemes: new Set(), lastOpenedURL: null, }); } }