import {AbsArgs} from 'scriptable-abstract'; import {MockNotification} from '../services/notification'; export interface ArgsMockState { plainTexts: string[]; urls: string[]; fileURLs: string[]; images: Image[]; queryParameters: Record; shortcutParameter: any; widgetParameter: any; notification: Notification; } const DEFAULT_STATE: ArgsMockState = { plainTexts: [], urls: [], fileURLs: [], images: [], queryParameters: {}, shortcutParameter: null, widgetParameter: null, notification: new MockNotification(), }; export class MockArgs extends AbsArgs { static get instance(): MockArgs { if (!this._instance) { this._instance = new MockArgs(); } return this._instance as MockArgs; } protected constructor() { super(DEFAULT_STATE); } static reset(): void { (this.instance as MockArgs).setState(DEFAULT_STATE); } get plainTexts(): string[] { return [...(this.state.plainTexts ?? [])]; } get urls(): string[] { return [...(this.state.urls ?? [])]; } get fileURLs(): string[] { return [...(this.state.fileURLs ?? [])]; } get images(): Image[] { return [...(this.state.images ?? [])]; } get queryParameters(): Record { return {...(this.state.queryParameters ?? {})}; } get shortcutParameter(): any { return this.state.shortcutParameter; } get widgetParameter(): any { return this.state.widgetParameter; } get notification(): Notification { return this.state.notification ?? new MockNotification(); } setState(newState: Partial): this { if (!newState) { throw new Error('State cannot be null or undefined'); } // Validate arrays and objects if ('plainTexts' in newState && !Array.isArray(newState.plainTexts)) { throw new Error('plainTexts must be an array'); } if ('urls' in newState && !Array.isArray(newState.urls)) { throw new Error('urls must be an array'); } if ('fileURLs' in newState && !Array.isArray(newState.fileURLs)) { throw new Error('fileURLs must be an array'); } if ('images' in newState && !Array.isArray(newState.images)) { throw new Error('images must be an array'); } if ('queryParameters' in newState && typeof newState.queryParameters !== 'object') { throw new Error('queryParameters must be an object'); } return super.setState(newState); } // Testing helpers setPlainTexts(texts: string[]): void { if (!Array.isArray(texts)) { throw new Error('texts must be an array'); } this.setState({plainTexts: [...texts]}); } setUrls(urls: string[]): void { if (!Array.isArray(urls)) { throw new Error('urls must be an array'); } this.setState({urls: [...urls]}); } setFileURLs(urls: string[]): void { if (!Array.isArray(urls)) { throw new Error('urls must be an array'); } this.setState({fileURLs: [...urls]}); } setImages(images: Image[]): void { if (!Array.isArray(images)) { throw new Error('images must be an array'); } this.setState({images: [...images]}); } setQueryParameters(params: Record): void { if (typeof params !== 'object') { throw new Error('params must be an object'); } this.setState({queryParameters: {...params}}); } setShortcutParameter(param: any): void { this.setState({shortcutParameter: param}); } setWidgetParameter(param: any): void { this.setState({widgetParameter: param}); } setNotification(notification: Notification): void { if (!notification) { throw new Error('notification cannot be null'); } this.setState({notification}); } }