UNPKG

scriptable-testlab

Version:

A lightweight, efficient tool designed to manage and update scripts for Scriptable.

136 lines 4.25 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { Preferences } from "./preferences"; import { GlobalMockManager } from "./registry"; import { SystemState } from "./system"; import { RuntimePresets } from "./types/runtime"; const _ScriptableRuntime = class _ScriptableRuntime { constructor() { __publicField(this, "preferences"); if (_ScriptableRuntime.instance) { throw new Error("ScriptableRuntime is a singleton. Use ScriptableRuntime.getInstance()"); } this.preferences = Preferences.getInstance(); } /** * Get the singleton instance of ScriptableRuntime */ static getInstance() { if (!_ScriptableRuntime.instance) { _ScriptableRuntime.instance = new _ScriptableRuntime(); } return _ScriptableRuntime.instance; } /** * Configure runtime with preset or custom configuration * @param config Runtime configuration or preset name */ configure(config) { if (typeof config === "string") { const preset = RuntimePresets[config]; if (!preset) { throw new Error(`Preset "${config}" not found`); } this.applyConfig(preset); } else { this.applyConfig(config); } } /** * Apply configuration to runtime * @param config Runtime configuration */ applyConfig(config) { if (config.device) { this.preferences.updateMock("device", config.device); } if (config.widget) { this.preferences.updateMock("config", config.widget); } if (config.script) { this.preferences.updateMock("script", config.script); } if (config.location) { this.preferences.updateMock("location", { currentLocation: config.location.currentLocation ?? { latitude: 0, longitude: 0, altitude: 0, horizontalAccuracy: 5, verticalAccuracy: 5 }, accuracy: config.location.accuracy ?? "best", simulatedDelay: config.location.simulatedDelay ?? 100 }); } } /** * Initialize the runtime and setup mocks * @throws Error if called while a test is running */ setupMocks() { this.assertTestNotRunning(); if (_ScriptableRuntime.isInitialized) { throw new Error("ScriptableRuntime is already initialized. Call clearMocks() first."); } GlobalMockManager.initialize(); this.preferences.reset(); _ScriptableRuntime.isInitialized = true; _ScriptableRuntime.isTestRunning = true; } /** * Clear all mocks and reset the runtime state * @throws Error if called while no test is running */ clearMocks() { SystemState.reset(); this.preferences.reset(); GlobalMockManager.clear(); _ScriptableRuntime.isInitialized = false; _ScriptableRuntime.isTestRunning = false; } assertTestNotRunning() { if (_ScriptableRuntime.isTestRunning) { throw new Error("A test is currently running. Call clearMocks() before starting a new test."); } } // #region UI State Shortcuts get isDarkMode() { return this.preferences.get().device.appearance?.isUsingDarkAppearance ?? false; } set isDarkMode(value) { this.preferences.updateMock("device", { appearance: { isUsingDarkAppearance: value } }); } get widgetFamily() { return this.preferences.get().config.widgetFamily ?? "small"; } set widgetFamily(value) { this.preferences.updateMock("config", { widgetFamily: value }); } get alertResponse() { return this.preferences.get().device.alertResponse; } set alertResponse(value) { this.preferences.updateMock("device", { alertResponse: value }); } // #endregion }; __publicField(_ScriptableRuntime, "instance"); __publicField(_ScriptableRuntime, "isInitialized", false); __publicField(_ScriptableRuntime, "isTestRunning", false); let ScriptableRuntime = _ScriptableRuntime; const runtime = ScriptableRuntime.getInstance(); export { ScriptableRuntime, runtime }; //# sourceMappingURL=runtime.js.map