import { platform } from 'node:process' import ci from 'is-ci' import type { VisOptions } from '../config/types.ts' import { SNAPSHOT_ROOT_DIR } from '../shared/constants.ts' import { trimCommonFolder } from '../shared/trim_common_folder.ts' import type { ExtendedBrowserCommandContext } from './vis_server_context.types.ts' export function resolveSnapshotRootDir(browserCommandContext: ExtendedBrowserCommandContext, options: VisOptions) { if (!options.snapshotRootDir) return getSnapshotRootDir() const snapshotRootDir = options.snapshotRootDir if (typeof snapshotRootDir === 'string') return getSnapshotRootDir(snapshotRootDir) const { config } = browserCommandContext.project.browser return snapshotRootDir({ ci, rootDir: SNAPSHOT_ROOT_DIR, projectName: browserCommandContext.project.name, browserName: config.browser.name, providerName: browserCommandContext.provider.name, platform, screenshotFailures: config.browser.screenshotFailures, screenshotDirectory: config.browser.screenshotDirectory, }) } /** * Gets the root directory for storing snapshots. * * @param snapshotRootDir - Base directory for snapshots. Defaults to `__vis__`. * @returns The full snapshot root directory path, which includes a platform-specific subdirectory: * - When running in CI: `/` (e.g. `__vis__/linux`) * - When running locally: `/local` */ export function getSnapshotRootDir(snapshotRootDir = SNAPSHOT_ROOT_DIR) { return `${snapshotRootDir}/${ci ? platform : 'local'}` } export function getSnapshotSubpath(suiteName: string, options: Pick) { const snapshotSubpath = options.snapshotSubpath ?? (({ subpath }) => trimCommonFolder(subpath)) return snapshotSubpath({ subpath: suiteName }) }