Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions e2e/lifecycle-delete-untrack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
try {
await access(p)
Expand All @@ -31,6 +33,13 @@ async function tileExists(installationId: string): Promise<boolean> {
return ctx.panel.exists(byTestId(TID.dashboardTile(installationId)))
}

async function errorShownOnDeletedBuild(): Promise<string | null> {
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<void> {
const kebabClicked = await ctx.panel.click(byTestId(TID.dashboardTileKebab(installationId)))
expect(kebabClicked, `kebab click on ${installationId}`).toBe(true)
Expand Down Expand Up @@ -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)
Expand Down
80 changes: 80 additions & 0 deletions src/main/installations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>; land: () => void } {
const realRename = fs.promises.rename.bind(fs.promises)
let markReached: () => void = () => {}
const reached = new Promise<void>((resolve) => {
markReached = resolve
})
let land: () => void = () => {}
const allowed = new Promise<void>((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'])
})
})
14 changes: 7 additions & 7 deletions src/main/installations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ function maybeSeedFromEnv(): void {
}
}

async function load(): Promise<InstallationRecord[]> {
return (await loadOutcome()).records
function loadAfterRequestedWrites(): Promise<InstallationRecord[]> {
return _queue.then(async () => (await loadOutcome()).records)
}

async function loadOutcome(): Promise<{ records: InstallationRecord[]; unreadable: boolean }> {
Expand Down Expand Up @@ -152,11 +152,11 @@ async function save(installations: InstallationRecord[]): Promise<void> {
}

export async function list(): Promise<InstallationRecord[]> {
return load()
return loadAfterRequestedWrites()
}

export async function hasNameConflict(id: string, name: string): Promise<boolean> {
const all = await load()
const all = await loadAfterRequestedWrites()
return all.some((i) => i.id !== id && i.name === name)
}

Expand Down Expand Up @@ -281,7 +281,7 @@ export async function associateUnownedBuildInstalls(
}

export async function get(id: string): Promise<InstallationRecord | null> {
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<void> {
Expand Down Expand Up @@ -376,7 +376,7 @@ export async function clearPendingTemplateOpen(installationId: string): Promise<
}

export async function getRecent(): Promise<InstallationRecord | null> {
const list = await load()
const list = await loadAfterRequestedWrites()
let best: InstallationRecord | null = null
let bestTs = -Infinity
for (const inst of list) {
Expand All @@ -393,7 +393,7 @@ export async function getRecentByCategory(
category: string,
resolveCategory: (inst: InstallationRecord) => string | undefined
): Promise<InstallationRecord | null> {
const list = await load()
const list = await loadAfterRequestedWrites()
let best: InstallationRecord | null = null
let bestTs = -Infinity
for (const inst of list) {
Expand Down
117 changes: 117 additions & 0 deletions src/main/lib/firstUseDetection.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown> {
return {
name: 'ComfyUI Legacy Desktop',
sourceId: 'desktop',
installPath: path.join(tmpRoot, 'legacy'),
launchMode: 'external',
status: 'installed'
}
}

function holdNextWriteAtItsRename(): { reached: Promise<void>; land: () => void } {
const realRename = fs.promises.rename.bind(fs.promises)
let markReached: () => void = () => {}
const reached = new Promise<void>((resolve) => {
markReached = resolve
})
let land: () => void = () => {}
const allowed = new Promise<void>((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 })
})
})
Loading