diff --git a/e2e/lifecycle-delete-untrack.test.ts b/e2e/lifecycle-delete-untrack.test.ts index 543223da9..a78098401 100644 --- a/e2e/lifecycle-delete-untrack.test.ts +++ b/e2e/lifecycle-delete-untrack.test.ts @@ -18,6 +18,8 @@ const DELETE_NAME = 'Delete Me' const MARKER_FILENAME = '.comfyui-desktop-2' +const DELETE_ERROR_BADGE = `${byTestId(TID.dashboardTile(DELETE_ID))} .chooser-tile-error-badge` + async function pathExists(p: string): Promise { try { await access(p) @@ -31,6 +33,13 @@ async function tileExists(installationId: string): Promise { return ctx.panel.exists(byTestId(TID.dashboardTile(installationId))) } +async function errorShownOnDeletedBuild(): Promise { + if (!(await ctx.panel.isVisible(DELETE_ERROR_BADGE))) return null + expect(await ctx.panel.click(DELETE_ERROR_BADGE), 'error badge click dispatched').toBe(true) + await ctx.panel.waitForVisible('.base-alert-message-text', { timeout: 5_000 }) + return ctx.panel.textOf('.base-alert-message-text') +} + async function openKebabAndClick(installationId: string, menuItemId: string): Promise { const kebabClicked = await ctx.panel.click(byTestId(TID.dashboardTileKebab(installationId))) expect(kebabClicked, `kebab click on ${installationId}`).toBe(true) @@ -123,10 +132,11 @@ test('kebab Delete drops the record AND removes the install directory @lifecycle const confirmed = await ctx.panel.click(byTestId(TID.baseAlertAction)) expect(confirmed, 'delete confirm click dispatched').toBe(true) - await ctx.panel.waitFor(async () => !(await tileExists(DELETE_ID)), { - timeout: 30_000, - message: 'deleted tile never disappeared from chooser' - }) + await ctx.panel.waitFor( + async () => !(await tileExists(DELETE_ID)) || (await ctx.panel.isVisible(DELETE_ERROR_BADGE)), + { timeout: 30_000, message: 'the delete neither removed the build nor reported an error' } + ) + expect(await errorShownOnDeletedBuild(), 'the app reported an error for the delete').toBeNull() await expect .poll(() => pathExists(deletePath), { timeout: 30_000, intervals: [250, 500, 1000] }) .toBe(false) diff --git a/src/main/installations.test.ts b/src/main/installations.test.ts index 92703af85..084aef2d8 100644 --- a/src/main/installations.test.ts +++ b/src/main/installations.test.ts @@ -916,3 +916,83 @@ describe('mutations fail closed when installations.json cannot be recovered (iss expect(fs.readFileSync(dataPath, 'utf-8')).toBe(corrupt) }) }) + +function holdNextWriteAtItsRename(): { reached: Promise; land: () => void } { + const realRename = fs.promises.rename.bind(fs.promises) + let markReached: () => void = () => {} + const reached = new Promise((resolve) => { + markReached = resolve + }) + let land: () => void = () => {} + const allowed = new Promise((resolve) => { + land = resolve + }) + vi.spyOn(fs.promises, 'rename').mockImplementationOnce(async (from, to) => { + markReached() + await allowed + return realRename(from, to) + }) + return { reached, land } +} + +describe('reading the list while a write requested earlier is still reaching the disk', () => { + it('lists a build that is still being added', async () => { + const installations = await loadInstallations() + const disk = holdNextWriteAtItsRename() + const adding = installations.ensureExists('desktop', { + name: 'ComfyUI Legacy Desktop', + sourceId: 'desktop', + installPath: path.join(tmpRoot, 'legacy'), + status: 'installed' + }) + const listed = installations.list() + + await disk.reached + disk.land() + + expect((await listed).map((inst) => inst.sourceId)).toEqual(['desktop']) + await adding + }) + + it('returns a build with a change that is still being saved', async () => { + const installations = await loadInstallations() + const entry = await installations.add({ + name: 'Local A', + installPath: path.join(tmpRoot, 'a'), + sourceId: 'standalone', + status: 'installed' + }) + const disk = holdNextWriteAtItsRename() + const renaming = installations.update(entry.id, { name: 'Renamed' }) + const fetched = installations.get(entry.id) + + await disk.reached + disk.land() + + expect((await fetched)!.name).toBe('Renamed') + await renaming + }) + + it('still answers after a write that failed', async () => { + const installations = await loadInstallations() + await installations.add({ + name: 'Local A', + installPath: path.join(tmpRoot, 'a'), + sourceId: 'standalone', + status: 'installed' + }) + const full = new Error('fake ENOSPC') as NodeJS.ErrnoException + full.code = 'ENOSPC' + vi.spyOn(fs.promises, 'rename').mockRejectedValueOnce(full) + const adding = installations.ensureExists('desktop', { + name: 'ComfyUI Legacy Desktop', + sourceId: 'desktop', + installPath: path.join(tmpRoot, 'legacy'), + status: 'installed' + }) + const listed = installations.list() + + await expect(adding).rejects.toThrow('fake ENOSPC') + expect((await listed).map((inst) => inst.name)).toEqual(['Local A']) + }) +}) diff --git a/src/main/installations.ts b/src/main/installations.ts index 1250b9842..07f7ca38e 100644 --- a/src/main/installations.ts +++ b/src/main/installations.ts @@ -113,8 +113,8 @@ function maybeSeedFromEnv(): void { } } -async function load(): Promise { - return (await loadOutcome()).records +function loadAfterRequestedWrites(): Promise { + return _queue.then(async () => (await loadOutcome()).records) } async function loadOutcome(): Promise<{ records: InstallationRecord[]; unreadable: boolean }> { @@ -152,11 +152,11 @@ async function save(installations: InstallationRecord[]): Promise { } export async function list(): Promise { - return load() + return loadAfterRequestedWrites() } export async function hasNameConflict(id: string, name: string): Promise { - const all = await load() + const all = await loadAfterRequestedWrites() return all.some((i) => i.id !== id && i.name === name) } @@ -281,7 +281,7 @@ export async function associateUnownedBuildInstalls( } export async function get(id: string): Promise { - return (await load()).find((i) => i.id === id) ?? null + return (await loadAfterRequestedWrites()).find((i) => i.id === id) ?? null } export async function reorder(orderedIds: string[]): Promise { @@ -376,7 +376,7 @@ export async function clearPendingTemplateOpen(installationId: string): Promise< } export async function getRecent(): Promise { - const list = await load() + const list = await loadAfterRequestedWrites() let best: InstallationRecord | null = null let bestTs = -Infinity for (const inst of list) { @@ -393,7 +393,7 @@ export async function getRecentByCategory( category: string, resolveCategory: (inst: InstallationRecord) => string | undefined ): Promise { - const list = await load() + const list = await loadAfterRequestedWrites() let best: InstallationRecord | null = null let bestTs = -Infinity for (const inst of list) { diff --git a/src/main/lib/firstUseDetection.test.ts b/src/main/lib/firstUseDetection.test.ts new file mode 100644 index 000000000..749f829b0 --- /dev/null +++ b/src/main/lib/firstUseDetection.test.ts @@ -0,0 +1,117 @@ +import fs from 'fs' +import os from 'os' +import path from 'path' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +let tmpRoot = '' +let userDataPath = '' + +async function loadStartup() { + const installations = await import('../installations') + const { detectFirstUseState } = await import('./firstUseDetection') + return { installations, detectFirstUseState } +} + +const CLOUD = { + name: 'Comfy Cloud', + sourceId: 'cloud', + remoteUrl: 'https://cloud.comfy.org/', + launchMode: 'window', + browserPartition: 'shared' +} + +function legacyDesktop(): Record { + return { + name: 'ComfyUI Legacy Desktop', + sourceId: 'desktop', + installPath: path.join(tmpRoot, 'legacy'), + launchMode: 'external', + status: 'installed' + } +} + +function holdNextWriteAtItsRename(): { reached: Promise; land: () => void } { + const realRename = fs.promises.rename.bind(fs.promises) + let markReached: () => void = () => {} + const reached = new Promise((resolve) => { + markReached = resolve + }) + let land: () => void = () => {} + const allowed = new Promise((resolve) => { + land = resolve + }) + vi.spyOn(fs.promises, 'rename').mockImplementationOnce(async (from, to) => { + markReached() + await allowed + return realRename(from, to) + }) + return { reached, land } +} + +beforeEach(() => { + tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'comfyui-desktop-2-first-use-')) + userDataPath = path.join(tmpRoot, 'user-data') + fs.mkdirSync(userDataPath, { recursive: true }) + + vi.resetModules() + vi.restoreAllMocks() + vi.doMock('electron', () => ({ + app: { + getPath: () => userDataPath + } + })) + vi.stubGlobal('process', { + ...process, + platform: 'win32' + }) +}) + +afterEach(() => { + vi.unstubAllGlobals() + vi.restoreAllMocks() + fs.rmSync(tmpRoot, { recursive: true, force: true }) +}) + +describe('what Set up is told about the builds already on this computer', () => { + it('has nothing to offer on a computer with only Comfy Cloud', async () => { + const { installations, detectFirstUseState } = await loadStartup() + await installations.seedDefaults([CLOUD]) + + expect(await detectFirstUseState()).toEqual({ skipPick: false, hasLegacyDesktop: false }) + }) + + it('offers to migrate a legacy ComfyUI Desktop install found at startup', async () => { + const { installations, detectFirstUseState } = await loadStartup() + await installations.seedDefaults([CLOUD]) + await installations.ensureExists('desktop', legacyDesktop()) + + expect(await detectFirstUseState()).toEqual({ skipPick: false, hasLegacyDesktop: true }) + }) + + it('offers to migrate a legacy install that startup is still adding to the builds list', async () => { + const { installations, detectFirstUseState } = await loadStartup() + await installations.seedDefaults([CLOUD]) + const disk = holdNextWriteAtItsRename() + const adding = installations.ensureExists('desktop', legacyDesktop()) + const asked = detectFirstUseState() + + await disk.reached + disk.land() + + expect(await asked).toEqual({ skipPick: false, hasLegacyDesktop: true }) + await adding + }) + + it('lets someone who already has a build keep it', async () => { + const { installations, detectFirstUseState } = await loadStartup() + await installations.seedDefaults([CLOUD]) + await installations.add({ + name: 'ComfyUI', + installPath: path.join(tmpRoot, 'build'), + sourceId: 'standalone', + status: 'installed' + }) + + expect(await detectFirstUseState()).toEqual({ skipPick: true, hasLegacyDesktop: false }) + }) +})