import {AbsConsole} from 'scriptable-abstract'; /** * State interface for Console mock */ interface ConsoleState { logs: Array<{ type: 'log' | 'warn' | 'error'; message: string; timestamp: number; }>; } /** * Mock implementation of Scriptable's Console global variable * @implements Console */ export class MockConsole extends AbsConsole { constructor() { super({ logs: [], }); } /** * @inheritdoc */ log(...values: any[]): void { const message = values.map(v => String(v)).join(' '); this.addLog('log', message); } /** * @inheritdoc */ warn(...values: any[]): void { const message = values.map(v => String(v)).join(' '); this.addLog('warn', message); } /** * @inheritdoc */ error(...values: any[]): void { const message = values.map(v => String(v)).join(' '); this.addLog('error', message); } /** * @additional * Clear all logs from the console state */ clear(): void { this.setState({ logs: [], }); } /** * @additional * Get all logs from the console state */ getLogs(): readonly string[] { return Object.freeze( this.state.logs.map(log => { switch (log.type) { case 'warn': return `[WARN] ${log.message}`; case 'error': return `[ERROR] ${log.message}`; default: return log.message; } }), ); } /** * @internal * Add a log entry to the state */ private addLog(type: 'log' | 'warn' | 'error', message: string): void { this.setState(state => ({ logs: [ ...state.logs, { type, message, timestamp: Date.now(), }, ], })); } }