mirror of
https://github.com/hanzoai/studio-desktop.git
synced 2026-10-30 18:21:16 +00:00
- Replace all ComfyUI references with Hanzo Studio - Update comfy.org URLs to hanzo.ai/studio.hanzo.ai - Change comfyanonymous to hanzoai - Update Comfy-Org to hanzo-studio - Rename files and directories - Update package names and configurations - Update documentation and README files This is a comprehensive rebranding to align with Hanzo AI's product suite and company identity.
152 lines
5.0 KiB
TypeScript
152 lines
5.0 KiB
TypeScript
import { readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
import fs from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import { getHanzo StudioAppDataPath, getDefaultInstallLocation, pathExists } from 'tests/shared/utils';
|
|
|
|
import { LogFile } from '@/constants';
|
|
import type { DesktopSettings } from '@/store/desktopSettings';
|
|
|
|
import { TempDirectory } from './tempDirectory';
|
|
import { assertPlaywrightEnabled } from './testExtensions';
|
|
|
|
export class TestEnvironment implements AsyncDisposable {
|
|
readonly appDataDir: string = getHanzo StudioAppDataPath();
|
|
readonly configPath: string = path.join(this.appDataDir, 'config.json');
|
|
|
|
readonly installLocation: TempDirectory = new TempDirectory();
|
|
readonly defaultInstallLocation: string = getDefaultInstallLocation();
|
|
|
|
readonly mainLogPath: string = path.join(this.appDataDir, 'logs', LogFile.Main);
|
|
readonly hanzo-studioLogPath: string = path.join(this.appDataDir, 'logs', LogFile.Hanzo Studio);
|
|
|
|
#haveBrokenInstallPath = false;
|
|
#haveBrokenVenv = false;
|
|
#haveBrokenServerStart = false;
|
|
|
|
#disposed: boolean = false;
|
|
|
|
constructor(
|
|
/** Set to `true` to automatically wipe all install data at test end. */
|
|
readonly destroyEnvironmentOnDispose: boolean = false
|
|
) {}
|
|
|
|
async readConfig() {
|
|
const config = await readFile(this.configPath, 'utf8');
|
|
return JSON.parse(config) as DesktopSettings;
|
|
}
|
|
|
|
async breakInstallPath() {
|
|
const config = await this.readConfig();
|
|
config.basePath = `${config.basePath}-invalid`;
|
|
await writeFile(this.configPath, JSON.stringify(config, null, 2), { flush: true });
|
|
this.#haveBrokenInstallPath = true;
|
|
}
|
|
|
|
async restoreInstallPath() {
|
|
if (!this.#haveBrokenInstallPath) return;
|
|
this.#haveBrokenInstallPath = false;
|
|
|
|
const config = await this.readConfig();
|
|
config.basePath = config.basePath?.replace(/-invalid$/, '');
|
|
await writeFile(this.configPath, JSON.stringify(config, null, 2), { flush: true });
|
|
}
|
|
|
|
async breakVenv() {
|
|
const venvPath = path.join(this.defaultInstallLocation, '.venv');
|
|
await rename(venvPath, `${venvPath}-invalid`);
|
|
this.#haveBrokenVenv = true;
|
|
}
|
|
|
|
async restoreVenv() {
|
|
if (!this.#haveBrokenVenv) return;
|
|
this.#haveBrokenVenv = false;
|
|
|
|
const venvPath = path.join(this.defaultInstallLocation, '.venv');
|
|
const invalidVenvExists = await pathExists(`${venvPath}-invalid`);
|
|
if (!invalidVenvExists) throw new Error('Invalid venv does not exist');
|
|
|
|
if (await pathExists(venvPath)) {
|
|
await rm(venvPath, { recursive: true, force: true });
|
|
}
|
|
await rename(`${venvPath}-invalid`, venvPath);
|
|
}
|
|
|
|
async breakServerStart() {
|
|
this.#haveBrokenServerStart = true;
|
|
try {
|
|
const filePath = path.join(this.defaultInstallLocation, 'user', 'default', 'comfy.settings.json');
|
|
const json = await fs.readFile(filePath, 'utf8');
|
|
|
|
const comfySettings = JSON.parse(json);
|
|
const launchArgs = comfySettings['Comfy.Server.LaunchArgs'];
|
|
if (!launchArgs) throw new Error('Could not reach launch args from comfy.settings.json');
|
|
|
|
delete launchArgs.cpu;
|
|
launchArgs['invalid-arg'] = '';
|
|
comfySettings['Comfy.Server.LaunchArgs'] = launchArgs;
|
|
|
|
await fs.writeFile(filePath, JSON.stringify(comfySettings, null, 2), { flush: true });
|
|
} catch (error) {
|
|
this.#haveBrokenServerStart = false;
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async restoreServerStart() {
|
|
if (!this.#haveBrokenServerStart) return;
|
|
this.#haveBrokenServerStart = false;
|
|
try {
|
|
const filePath = path.join(this.defaultInstallLocation, 'user', 'default', 'comfy.settings.json');
|
|
const json = await fs.readFile(filePath, 'utf8');
|
|
|
|
const comfySettings = JSON.parse(json);
|
|
comfySettings['Comfy.Server.LaunchArgs'].cpu = '';
|
|
delete comfySettings['Comfy.Server.LaunchArgs']['invalid-arg'];
|
|
|
|
await fs.writeFile(filePath, JSON.stringify(comfySettings, null, 2), { flush: true });
|
|
} catch (error) {
|
|
this.#haveBrokenServerStart = true;
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async deleteEverything() {
|
|
console.warn('Playwright test environment clean up: deleteEverything');
|
|
await this.deleteAppData();
|
|
await this.deleteInstallLocation();
|
|
}
|
|
|
|
async deleteAppData() {
|
|
assertPlaywrightEnabled();
|
|
await rm(this.appDataDir, { recursive: true, force: true });
|
|
}
|
|
|
|
async deleteInstallLocation() {
|
|
assertPlaywrightEnabled();
|
|
await this.installLocation[Symbol.asyncDispose]();
|
|
}
|
|
|
|
async deleteDefaultInstallLocation() {
|
|
assertPlaywrightEnabled();
|
|
await rm(this.defaultInstallLocation, { recursive: true, force: true });
|
|
}
|
|
|
|
async deleteLogsIfPresent() {
|
|
assertPlaywrightEnabled();
|
|
await rm(this.mainLogPath, { force: true });
|
|
await rm(this.hanzo-studioLogPath, { force: true });
|
|
}
|
|
|
|
async [Symbol.asyncDispose]() {
|
|
if (this.#disposed) return;
|
|
this.#disposed = true;
|
|
|
|
if (this.destroyEnvironmentOnDispose) await this.deleteEverything();
|
|
|
|
await this.restoreInstallPath();
|
|
await this.restoreVenv();
|
|
await this.restoreServerStart();
|
|
await this.installLocation[Symbol.asyncDispose]();
|
|
}
|
|
}
|