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
26 changes: 26 additions & 0 deletions src/platform/workspace/api/workspaceApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,32 @@ describe('workspaceApi', () => {
)
})

it('forwards a clear cancellation to the transport', async () => {
mockAxiosInstance.delete.mockResolvedValue({})
const controller = new AbortController()
await workspaceApi.clearDeployment('ws-1', { signal: controller.signal })
expect(mockAxiosInstance.delete).toHaveBeenCalledWith(
'/api/workspaces/ws-1/deployment',
{ headers: AUTH_HEADER, params: undefined, signal: controller.signal }
)
})

it('does not send a canceled clear when authorization finishes late', async () => {
let finishAuth!: (headers: typeof AUTH_HEADER) => void
const pendingAuth = new Promise<typeof AUTH_HEADER>((resolve) => {
finishAuth = resolve
})
vi.mocked(auth.getWorkspaceAuthHeader).mockReturnValue(pendingAuth)
const controller = new AbortController()
const cleared = workspaceApi.clearDeployment('ws-1', {
signal: controller.signal
})
controller.abort()
finishAuth(AUTH_HEADER)
await expect(cleared).rejects.toMatchObject({ name: 'AbortError' })
expect(mockAxiosInstance.delete).not.toHaveBeenCalled()
})

it('setDefaultDeployment() sends PUT /workspaces/:id/default-deployment with the deployment', async () => {
mockAxiosInstance.put.mockResolvedValue({})

Expand Down
6 changes: 4 additions & 2 deletions src/platform/workspace/api/workspaceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,17 @@ export const workspaceApi = {
},
async clearDeployment(
workspaceId: WorkspaceId,
options: { follow?: 'workspace' } = {}
options: { follow?: 'workspace'; signal?: AbortSignal } = {}
): Promise<void> {
const auth = await requestAuth()
options.signal?.throwIfAborted()
try {
await workspaceApiClient.delete(
ingestApiUrl(`/workspaces/${workspaceId}/deployment`),
{
...auth,
params: options.follow ? { follow: options.follow } : undefined
params: options.follow ? { follow: options.follow } : undefined,
...(options.signal ? { signal: options.signal } : {})
}
)
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,76 @@ describe('useDeploymentUrlLoader', () => {
expect(useDeploymentPickStore().pickFromLink).toHaveBeenCalledWith('dep-1')
})

it('picks Comfy Cloud itself for deployment=standard, keeping the folder', async () => {
mockRouteQuery.value = {
workspace: 'ws-1',
deployment: 'standard',
workflows: 'build'
}

const { loadDeploymentFromUrl } = useDeploymentUrlLoader()
await expect(loadDeploymentFromUrl()).resolves.toBe(true)

expect(mockRouterReplace).toHaveBeenCalledWith({
query: { workflows: 'build' }
})
expect(useDeploymentPickStore().pickFromLink).toHaveBeenCalledWith(null)
})

it('says nothing for deployment=standard when the browser is on Comfy Cloud', async () => {
mockRouteQuery.value = { workspace: 'ws-1', deployment: 'standard' }
vi.mocked(useDeploymentPickStore().pickFromLink).mockResolvedValue({
kind: 'unchanged'
})

await expect(
useDeploymentUrlLoader().loadDeploymentFromUrl()
).resolves.toBe(false)

expect(useDeploymentPickStore().pickFromLink).toHaveBeenCalledWith(null)
expect(mockRouterReplace).toHaveBeenCalledWith({ query: {} })
expect(useToast().error).not.toHaveBeenCalled()
})

it('shows the timed-out message when deployment=standard does not land', async () => {
mockRouteQuery.value = { workspace: 'ws-1', deployment: 'standard' }
vi.mocked(useDeploymentPickStore().pickFromLink).mockResolvedValue({
kind: 'timedOut'
})

await expect(
useDeploymentUrlLoader().loadDeploymentFromUrl()
).resolves.toBe(false)

expect(useToast().error).toHaveBeenCalledWith(
'This deployment took too long to open. Try the link again.',
{ duration: 8000 }
)
})

it('switches workspace for deployment=standard, then picks Comfy Cloud', async () => {
mockRouteQuery.value = {
workspace: 'ws-2',
deployment: 'standard',
workflows: 'build'
}

await expect(
useDeploymentUrlLoader().loadDeploymentFromUrl()
).resolves.toBe(true)
expect(useTeamWorkspaceStore().switchWorkspace).toHaveBeenCalledWith('ws-2')
expect(mockRouterReplace).not.toHaveBeenCalled()

Object.assign(useTeamWorkspaceStore(), { activeWorkspace: studio })
await expect(
useDeploymentUrlLoader().loadDeploymentFromUrl()
).resolves.toBe(true)
expect(mockRouterReplace).toHaveBeenCalledWith({
query: { workflows: 'build' }
})
expect(useDeploymentPickStore().pickFromLink).toHaveBeenCalledWith(null)
})

it('waits for URL cleanup to complete before starting the pick', async () => {
mockRouteQuery.value = { workspace: 'ws-1', deployment: 'dep-1' }
let finishCleanup!: () => void
Expand Down
29 changes: 8 additions & 21 deletions src/platform/workspace/composables/useDeploymentUrlLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const WORKSPACE_NAMESPACE = PRESERVED_QUERY_NAMESPACES.WORKSPACE
const DEPLOYMENT_NAMESPACE = PRESERVED_QUERY_NAMESPACES.DEPLOYMENT
const SWITCH_KEY = WORKSPACE_STORAGE_KEYS.DEPLOYMENT_LINK_SWITCH

const COMFY_CLOUD_LINK_VALUE = 'standard'

/**
* One usable value: a non-empty string. Null for absent, empty, a bare
* `?deployment` (null), and a repeated value (an array, or the stash's `,`
Expand Down Expand Up @@ -70,21 +72,8 @@ function setSwitchTarget(workspaceId: string | null): boolean {
}

/**
* Opens the editor on the deployment a link names (BE-20294):
* `/?workspace=<workspace id>&deployment=<deployment id>`. Runs before the
* `?workspace=` loader and owns both params when `deployment` is present.
*
* In another workspace it switches there first, keeping both params so the
* boot after the switch's reload picks; a switch that fails, or that did not
* land after its reload, stays in the active workspace and says so. In the
* named workspace it strips both params, then picks with the switcher's own
* request, which reloads onto the deployment's nodes; a refused pick (outside
* the rollout, a deployment gone or not the workspace's) shows the server's
* reason and changes nothing. A link without both values, or an API-key
* session (bound to one workspace, no pick), does nothing but drop
* `deployment`. Signed out, the router sends the person to sign in with the
* full path, query included, to come back to; both params are also stashed
* by the preserved-query tracker.
* Opens the deployment, or Comfy Cloud for `standard`, that a
* `?workspace=<id>&deployment=<id>` link names.
*/
export function useDeploymentUrlLoader() {
const route = useRoute()
Expand Down Expand Up @@ -149,16 +138,14 @@ export function useDeploymentUrlLoader() {
return false
}

/**
* In the named workspace: strip the link, then pick. Returns `true` when
* the pick took and the page is reloading onto the deployment.
*/
async function pickDeployment(
query: LocationQueryRaw,
deploymentId: string
linkValue: string
): Promise<boolean> {
await stripParams(query, ['workspace', 'deployment'])
const outcome = await pickStore.pickFromLink(deploymentId)
const outcome = await pickStore.pickFromLink(
linkValue === COMFY_CLOUD_LINK_VALUE ? null : linkValue
)
if (outcome.kind === 'timedOut') {
toast.error(t('deploymentSwitcher.linkTimedOut'), { duration: 8000 })
}
Expand Down
141 changes: 141 additions & 0 deletions src/platform/workspace/stores/deploymentPickStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,147 @@ describe('useDeploymentPickStore', () => {
})
})

describe('a link to Comfy Cloud itself', () => {
it("sends the switcher's Comfy Cloud request after the listing, then reloads", async () => {
mockWorkspaceApi.listDeployments.mockResolvedValue(listing)
mockWorkspaceApi.clearDeployment.mockResolvedValue(undefined)
const store = useDeploymentPickStore()

await expect(store.pickFromLink(null)).resolves.toEqual({
kind: 'picked'
})

expect(mockWorkspaceApi.listDeployments).toHaveBeenCalledOnce()
expect(mockWorkspaceApi.clearDeployment).toHaveBeenCalledExactlyOnceWith(
'ws-1',
{ signal: expect.any(AbortSignal) }
)
expect(mockWorkspaceApi.pickDeployment).not.toHaveBeenCalled()
expect(readDeploymentHint('ws-1')).toBe('cloud')
expect(reload).toHaveBeenCalledOnce()
})

it.for([
{
name: 'no pick and no default',
answer: {
...listing,
picked_deployment_id: undefined,
pick_source: undefined
}
},
{
name: 'its own pick of Comfy Cloud',
answer: { ...listing, picked_deployment_id: undefined }
},
{
name: 'a default that is gone',
answer: {
...listing,
picked_deployment_id: undefined,
pick_source: 'workspace_default' as const,
gone_default_deployment_id: 'dep-gone'
}
}
])(
'does nothing when the browser already runs on Comfy Cloud: $name',
async ({ answer }) => {
mockWorkspaceApi.listDeployments.mockResolvedValue(answer)
const store = useDeploymentPickStore()

await expect(store.pickFromLink(null)).resolves.toEqual({
kind: 'unchanged'
})
expect(mockWorkspaceApi.clearDeployment).not.toHaveBeenCalled()
expect(reload).not.toHaveBeenCalled()
}
)

it('does nothing when the account cannot pick a deployment', async () => {
mockWorkspaceApi.listDeployments.mockRejectedValue(
new WorkspaceApiError('not enabled for this account yet', 403)
)
const store = useDeploymentPickStore()

await expect(store.pickFromLink(null)).resolves.toEqual({
kind: 'unchanged'
})
expect(mockWorkspaceApi.clearDeployment).not.toHaveBeenCalled()
expect(reload).not.toHaveBeenCalled()
})

it('still asks when the listing failed, since the pick is unknown', async () => {
mockWorkspaceApi.listDeployments.mockRejectedValue(
new WorkspaceApiError('Bad request', 400)
)
mockWorkspaceApi.clearDeployment.mockResolvedValue(undefined)
const store = useDeploymentPickStore()

await expect(store.pickFromLink(null)).resolves.toEqual({
kind: 'picked'
})
expect(mockWorkspaceApi.clearDeployment).toHaveBeenCalledOnce()
expect(reload).toHaveBeenCalledOnce()
})

it('stops waiting for a stalled listing and never asks when it answers', async () => {
vi.useFakeTimers()
const listed = deferred<WorkspaceDeploymentList>()
mockWorkspaceApi.listDeployments.mockReturnValue(listed.promise)
const store = useDeploymentPickStore()
const settled = vi.fn()
const linked = store.pickFromLink(null).then(settled)
await vi.advanceTimersByTimeAsync(DEPLOYMENT_LISTING_BACKSTOP_MS)
expect(settled).toHaveBeenCalledWith({ kind: 'timedOut' })
listed.resolve(listing)
await linked
expect(mockWorkspaceApi.clearDeployment).not.toHaveBeenCalled()
expect(reload).not.toHaveBeenCalled()
})

it('times out and cancels a stalled request without reloading on late success', async () => {
vi.useFakeTimers()
const cleared = deferred<void>()
let signal: AbortSignal | undefined
mockWorkspaceApi.listDeployments.mockResolvedValue(listing)
mockWorkspaceApi.clearDeployment.mockImplementation(
(_id: string, options?: { signal?: AbortSignal }) => {
signal = options?.signal
return cleared.promise
}
)
const store = useDeploymentPickStore()
const settled = vi.fn()
const linked = store.pickFromLink(null).then(settled)
await vi.advanceTimersByTimeAsync(0)
expect(mockWorkspaceApi.clearDeployment).toHaveBeenCalledOnce()
await vi.advanceTimersByTimeAsync(DEPLOYMENT_LISTING_BACKSTOP_MS)
expect(settled).toHaveBeenCalledWith({ kind: 'timedOut' })
expect(signal?.aborted).toBe(true)
expect(store.isSwitching).toBe(false)
cleared.resolve()
await linked
await vi.advanceTimersByTimeAsync(0)
expect(reload).not.toHaveBeenCalled()
})

it("hands back the server's refusal and keeps the listing's pick", async () => {
mockWorkspaceApi.listDeployments.mockResolvedValue(listing)
mockWorkspaceApi.clearDeployment.mockRejectedValue(
new WorkspaceApiError('workspace not found', 404)
)
const store = useDeploymentPickStore()

await expect(store.pickFromLink(null)).resolves.toEqual({
kind: 'refused',
message: 'workspace not found'
})
expect(store.state.phase).toBe('ready')
expect(store.pickedDeploymentId).toBe('dep-2')
expect(reload).not.toHaveBeenCalled()
})
})

it('does nothing without an active workspace', async () => {
Object.assign(useTeamWorkspaceStore(), { workspaceId: null })
const store = useDeploymentPickStore()
Expand Down
Loading
Loading