UNPKG

scriptable-testlab

Version:

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

194 lines (177 loc) 3.97 kB
import {AbsPhotos} from 'scriptable-abstract'; import {MockImage} from '../media/image'; /** * State interface for Photos mock */ interface PhotosState { library: { photos: MockImage[]; screenshots: MockImage[]; }; } /** * Mock implementation of Scriptable's Photos global variable. * Provides functionality for accessing and managing photos and screenshots in the device's library. * * @example * ```typescript * // Get latest photo from library * const photo = await Photos.latestPhoto(); * * // Take a photo with camera * const newPhoto = await Photos.fromCamera(); * * // Get latest screenshots * const screenshots = await Photos.latestScreenshots(5); * ``` * * @implements Photos */ export class MockPhotos extends AbsPhotos<PhotosState> { constructor() { super({ library: { photos: [], screenshots: [], }, }); } /** * @inheritdoc */ async fromLibrary(): Promise<MockImage> { if (this.state.library.photos.length === 0) { throw new Error('No photos in library'); } return this.state.library.photos[0]; } /** * @inheritdoc */ async fromCamera(): Promise<MockImage> { const image = new MockImage(); this.setState(state => ({ library: { ...state.library, photos: [image, ...state.library.photos], }, })); return image; } /** * @inheritdoc */ async latestPhoto(): Promise<MockImage> { if (this.state.library.photos.length === 0) { throw new Error('No photos in library'); } return this.state.library.photos[0]; } /** * @inheritdoc */ async latestPhotos(count: number): Promise<MockImage[]> { return this.state.library.photos.slice(0, count); } /** * @inheritdoc */ async latestScreenshot(): Promise<MockImage> { if (this.state.library.screenshots.length === 0) { throw new Error('No screenshots in library'); } return this.state.library.screenshots[0]; } /** * @inheritdoc */ async latestScreenshots(count: number): Promise<MockImage[]> { return this.state.library.screenshots.slice(0, count); } /** * @inheritdoc */ removeLatestPhoto(): void { if (this.state.library.photos.length === 0) { throw new Error('No photos in library'); } this.setState(state => ({ library: { ...state.library, photos: state.library.photos.slice(1), }, })); } /** * @inheritdoc */ removeLatestPhotos(count: number): void { this.setState(state => ({ library: { ...state.library, photos: state.library.photos.slice(count), }, })); } /** * @inheritdoc */ removeLatestScreenshot(): void { if (this.state.library.screenshots.length === 0) { throw new Error('No screenshots in library'); } this.setState(state => ({ library: { ...state.library, screenshots: state.library.screenshots.slice(1), }, })); } /** * @inheritdoc */ removeLatestScreenshots(count: number): void { this.setState(state => ({ library: { ...state.library, screenshots: state.library.screenshots.slice(count), }, })); } /** * @inheritdoc */ save(image: MockImage): void { this.setState(state => ({ library: { ...state.library, photos: [image, ...state.library.photos], }, })); } /** * @additional * Add a screenshot to the library * @param image The screenshot image to add */ addScreenshot(image: MockImage): void { this.setState(state => ({ library: { ...state.library, screenshots: [image, ...state.library.screenshots], }, })); } /** * @additional * Clear all photos and screenshots from the library */ clear(): void { this.setState({ library: { photos: [], screenshots: [], }, }); } }