scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
85 lines (75 loc) • 1.82 kB
text/typescript
import {AbsSafari} from 'scriptable-abstract';
type OpenMethod = 'browser' | 'app';
interface SafariState {
currentURL: string | null;
inBackground: boolean;
openMethod: OpenMethod | null;
fullscreen: boolean;
}
const DEFAULT_STATE: SafariState = {
currentURL: null,
inBackground: false,
openMethod: null,
fullscreen: false,
};
/**
* Mock implementation of Scriptable's Safari global variable.
* Provides functionality for opening URLs.
*
* @example
* ```typescript
* // Open a URL in Safari
* Safari.open('https://example.com');
*
* // Open a URL in Safari app
* await Safari.openInApp('https://example.com');
* ```
*
* @implements Safari
*/
export class MockSafari extends AbsSafari<SafariState> {
private static readonly VALID_SCHEMES = ['http:', 'https:'];
static get instance(): MockSafari {
return super.instance as MockSafari;
}
constructor() {
super(DEFAULT_STATE);
}
/**
* Validates if the given URL is valid and has an acceptable scheme
*/
private validateURL(url: string): void {
try {
const parsedURL = new URL(url);
if (!MockSafari.VALID_SCHEMES.includes(parsedURL.protocol)) {
throw new Error(`Invalid URL scheme: ${parsedURL.protocol}`);
}
} catch (error) {
throw new Error(`Invalid URL: ${error.message}`);
}
}
/**
* @inheritdoc
*/
open(url: string): void {
this.validateURL(url);
this.setState({
currentURL: url,
inBackground: false,
openMethod: 'browser',
fullscreen: false,
});
}
/**
* @inheritdoc
*/
async openInApp(url: string, fullscreen: boolean = false): Promise<void> {
this.validateURL(url);
this.setState({
currentURL: url,
inBackground: false,
openMethod: 'app',
fullscreen,
});
}
}