diff --git a/apps/website/e2e/workshop-workspace-switch.spec.ts b/apps/website/e2e/workshop-workspace-switch.spec.ts index a5d69bf9902..a5ca6f91261 100644 --- a/apps/website/e2e/workshop-workspace-switch.spec.ts +++ b/apps/website/e2e/workshop-workspace-switch.spec.ts @@ -1,3 +1,4 @@ +import type { Page } from '@playwright/test' import { expect } from '@playwright/test' import type { @@ -31,10 +32,7 @@ const WORKSPACES: ListWorkspacesResponse = { const TEAM = WORKSPACES.workspaces[1] -test('switching workspace during a run asks before it throws the run away', async ({ - page, - modelsAccount -}) => { +async function mockWorkspaces(page: Page) { await page.route('**/api/workspaces', (route) => route.fulfill({ status: 200, @@ -63,34 +61,56 @@ test('switching workspace during a run asks before it throws the run away', asyn } satisfies ExchangeTokenResponse) }) }) - // The run never answers, so it is still going when the workspace changes. - await page.route('**/v2/models/**', () => {}) +} +async function signIn( + page: Page, + account: { email: string; password: string } +) { await page.goto('/login/') await page.getByRole('button', { name: 'Use email instead' }).click() - await page.getByLabel('Email').fill(modelsAccount.email) - await page - .getByLabel('Password', { exact: true }) - .fill(modelsAccount.password) + await page.getByLabel('Email').fill(account.email) + await page.getByLabel('Password', { exact: true }).fill(account.password) await page.getByRole('button', { name: 'Sign in', exact: true }).click() await expect(page).toHaveURL('/') +} +async function startRun(page: Page) { await page.goto(MODEL_PATH) await page.getByTestId('field-prompt').fill('A teapot') await page.getByTestId('run-button').click() const output = page.getByTestId('playground-output') await expect(output).toHaveAttribute('data-state', 'running') + return output +} - const account = page +function accountMenu(page: Page) { + const trigger = page .getByTestId('desktop-nav-cta') .getByTestId('header-account') - const otherWorkspace = async () => { - await account.click() - await page.getByTestId('account-workspace').click() - await page.getByTestId('account-workspace-ws-team').click() + return { + open: () => trigger.click(), + pickTeam: async () => { + await trigger.click() + await page.getByTestId('account-workspace').click() + await page.getByTestId(`account-workspace-${TEAM.id}`).click() + } } +} + +test('switching workspace during a run asks before it throws the run away', async ({ + page, + modelsAccount +}) => { + await mockWorkspaces(page) + // The run never answers, so it is still going when the workspace changes. + await page.route('**/v2/models/**', () => {}) - await otherWorkspace() + await signIn(page, modelsAccount) + const output = await startRun(page) + const menu = accountMenu(page) + + await menu.pickTeam() const dialog = page.getByTestId('run-leave-dialog') await expect(dialog).toBeVisible() @@ -98,11 +118,50 @@ test('switching workspace during a run asks before it throws the run away', asyn await expect(dialog).toBeHidden() await expect(output).toHaveAttribute('data-state', 'running') - await otherWorkspace() + await menu.pickTeam() await page.getByTestId('run-leave-confirm').click() await expect(output).toHaveAttribute('data-state', 'cancelled') - await account.click() + await menu.open() + await expect(page.getByTestId('account-workspace-current')).toContainText( + TEAM.name + ) +}) + +test('a run that ends under the question answers it', async ({ + page, + modelsAccount +}) => { + await mockWorkspaces(page) + // The run is held until the dialog is up, then it ends on its own. + let land = () => {} + const held = new Promise((resolve) => { + land = resolve + }) + await page.route('**/v2/models/**', async (route) => { + await held + await route.fulfill({ + status: 503, + headers: { 'X-Comfy-Error-Type': 'provider_error' }, + contentType: 'application/json', + body: JSON.stringify({ detail: 'Temporary provider error' }) + }) + }) + + await signIn(page, modelsAccount) + const output = await startRun(page) + const menu = accountMenu(page) + + await menu.pickTeam() + const dialog = page.getByTestId('run-leave-dialog') + await expect(dialog).toBeVisible() + + land() + + await expect(output).toHaveAttribute('data-state', 'failed') + await expect(dialog).toBeHidden() + + await menu.open() await expect(page.getByTestId('account-workspace-current')).toContainText( TEAM.name ) diff --git a/apps/website/e2e/workshop.spec.ts b/apps/website/e2e/workshop.spec.ts index 268c858bbea..3f2dfffc1e7 100644 --- a/apps/website/e2e/workshop.spec.ts +++ b/apps/website/e2e/workshop.spec.ts @@ -638,6 +638,48 @@ test.describe('Model playground', () => { .poll(() => list.evaluate((el) => el.scrollWidth > el.clientWidth)) .toBe(true) }) + + // 320px is the narrowest phone the site supports, and it is where a fixed + // card width ran the next sample off the screen: a strip that scrolls with + // nothing showing past its edge reads as a single card. + test('the next sample shows past the edge at 320px @mobile', async ({ + page + }) => { + const width = 320 + await page.setViewportSize({ width, height: 720 }) + await page.goto('/models/krea--krea-2-medium-turbo--generate-images/') + const cards = page.getByTestId('example-card') + await expect(cards).toHaveCount(3) + + await expect + .poll(async () => { + const box = await cards.nth(1).boundingBox() + if (!box) return false + // Far enough in to be seen, and still running off the edge: a card + // that fitted whole would say the strip ends there. + return box.x < width - 24 && box.x + box.width > width + }) + .toBe(true) + }) + + test('a lone sample takes the phone row @mobile', async ({ page }) => { + await page.goto('/models/bfl--flux-2-pro--generate-images/') + const cards = page.getByTestId('example-card') + await expect(cards).toHaveCount(1) + + // The strip runs edge to edge behind a gutter of 24px on each side. + const list = page.getByTestId('examples-tab').locator('ul') + await expect + .poll(async () => { + const [listBox, cardBox] = await Promise.all([ + list.boundingBox(), + cards.first().boundingBox() + ]) + if (!listBox || !cardBox) return false + return Math.abs(cardBox.width - (listBox.width - 48)) < 2 + }) + .toBe(true) + }) }) test.describe('Filter sheet @mobile', () => { diff --git a/apps/website/src/components/ui/copy-text-button/CopyTextButton.test.ts b/apps/website/src/components/ui/copy-text-button/CopyTextButton.test.ts deleted file mode 100644 index 604a04f4fad..00000000000 --- a/apps/website/src/components/ui/copy-text-button/CopyTextButton.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { render, screen } from '@testing-library/vue' -import { describe, expect, it } from 'vitest' - -import CopyTextButton from './CopyTextButton.vue' - -function renderIcon(iconClass?: string) { - render(CopyTextButton, { - props: { - value: 'req-1', - label: 'Copy request ID', - copiedLabel: 'Copied', - iconClass - } - }) - return screen.getByTestId('copy-text-icon') -} - -describe('CopyTextButton', () => { - it('sizes its icon for the button it is, when nobody asks for anything else', () => { - expect(renderIcon()).toHaveClass('size-5') - }) - - it("takes the caller's size in place of its own, not beside it", () => { - const icon = renderIcon('size-3.5') - expect(icon).toHaveClass('size-3.5') - expect(icon).not.toHaveClass('size-5') - }) -}) diff --git a/apps/website/src/components/ui/copy-text-button/CopyTextButton.vue b/apps/website/src/components/ui/copy-text-button/CopyTextButton.vue index d95a00e3201..41b3b7a7952 100644 --- a/apps/website/src/components/ui/copy-text-button/CopyTextButton.vue +++ b/apps/website/src/components/ui/copy-text-button/CopyTextButton.vue @@ -41,11 +41,7 @@ const { copy, copied } = useClipboard({ copiedDuring: 2000 }) " @click="void copy(value)" > - + {{ copiedLabel }} diff --git a/apps/website/src/components/workshop/ExamplesTab.test.ts b/apps/website/src/components/workshop/ExamplesTab.test.ts index a21ee2512d9..de73b27d9b3 100644 --- a/apps/website/src/components/workshop/ExamplesTab.test.ts +++ b/apps/website/src/components/workshop/ExamplesTab.test.ts @@ -22,28 +22,3 @@ it('keeps the audio transport separate from the preset action', async () => { await user.click(preset) expect(emitted().open).toEqual([[example]]) }) - -// happy-dom lays nothing out, so the width can only be read off the class here. -// That it comes out to a judgeable size on a real phone is the @mobile spec in -// e2e/workshop.spec.ts; this pins which of the two widths each case picks. -it.for([ - [1, 'w-full'], - [3, 'w-72'] -] as const)('gives %i sample(s) the %s phone width', ([count, width]) => { - render(ExamplesTab, { - props: { - examples: Array.from({ length: count }, (_, index) => ({ - id: `sample-${index}`, - title: `Sample ${index}`, - specs: [], - values: {}, - outputUrl: `https://assets.example/sample-${index}.png`, - mediaKind: 'image' as const - })) - } - }) - - const items = screen.getAllByTestId('example-item') - expect(items).toHaveLength(count) - expect(items.every((item) => item.className.includes(width))).toBe(true) -}) diff --git a/apps/website/src/components/workshop/ExamplesTab.vue b/apps/website/src/components/workshop/ExamplesTab.vue index 35d314c8795..6fe5486db1c 100644 --- a/apps/website/src/components/workshop/ExamplesTab.vue +++ b/apps/website/src/components/workshop/ExamplesTab.vue @@ -27,8 +27,11 @@ const samplesOnly = computed( ) // A sample exists to be judged, and on a phone it was 144px wide: an 81px // preview of a generated image decides nothing. A lone sample takes the row, -// and several leave the next one peeking so the strip still reads as one. -const phoneWidth = computed(() => (examples.length === 1 ? 'w-full' : 'w-72')) +// and several take four fifths of it, so the next one peeks in at every width +// the phone layout covers, up to the 18rem past which a card gains nothing. +const phoneWidth = computed(() => + examples.length === 1 ? 'w-full' : 'w-4/5 max-sm:max-w-72' +) const desktopGridColumns = computed(() => examples.length === 3 ? 'sm:grid-cols-[repeat(auto-fit,minmax(14rem,1fr))]' diff --git a/apps/website/src/components/workshop/HeaderAccount.test.ts b/apps/website/src/components/workshop/HeaderAccount.test.ts index c0f1b65d602..5ec653be405 100644 --- a/apps/website/src/components/workshop/HeaderAccount.test.ts +++ b/apps/website/src/components/workshop/HeaderAccount.test.ts @@ -520,6 +520,123 @@ describe('HeaderAccount workspace switcher', () => { ) }) + it('carries the switch through when the run ends under the dialog', async () => { + signIn() + const cancel = vi.fn() + reportWorkshopRun(cancel) + onTestFinished(() => reportWorkshopRun(undefined)) + vi.stubGlobal( + 'fetch', + vi + .fn() + .mockResolvedValue( + new Response(JSON.stringify(listing), { status: 200 }) + ) + ) + const user = userEvent.setup() + render(HeaderAccount) + + await openSwitcher(user) + await user.click(await screen.findByTestId('account-workspace-team-1')) + expect(await screen.findByTestId('run-leave-dialog')).toBeTruthy() + + reportWorkshopRun(undefined) + + await waitFor(() => + expect(h.remint).toHaveBeenCalledWith(undefined, { + workspaceId: 'team-1', + preserveCredentialOnTransientFailure: true + }) + ) + expect(cancel).not.toHaveBeenCalled() + await waitFor(() => + expect(screen.queryByTestId('run-leave-dialog')).toBeNull() + ) + }) + + it('drops a pending switch when the authenticated session changes', async () => { + signIn() + const cancel = vi.fn() + reportWorkshopRun(cancel) + onTestFinished(() => reportWorkshopRun(undefined)) + vi.stubGlobal( + 'fetch', + vi + .fn() + .mockResolvedValue( + new Response(JSON.stringify(listing), { status: 200 }) + ) + ) + const user = userEvent.setup() + render(HeaderAccount) + + await openSwitcher(user) + await user.click(await screen.findByTestId('account-workspace-team-1')) + expect(await screen.findByTestId('run-leave-dialog')).toBeTruthy() + + const replacement = { + token: 'other-jwt', + uid: 'user-2', + workspace: { id: 'other', name: 'Other', type: 'personal' as const }, + role: 'owner' + } + h.user!.value = { + uid: 'user-2', + email: 'other@b.co', + displayName: 'Other' + } + h.session!.value = replacement + reportWorkshopRun(undefined) + + await waitFor(() => + expect(screen.queryByTestId('run-leave-dialog')).toBeNull() + ) + expect(h.remint).not.toHaveBeenCalled() + expect(cancel).not.toHaveBeenCalled() + expect(h.session!.value).toEqual(replacement) + }) + + // The other arm of the same scope check: the reader stays signed in, and only + // the workspace moves under the dialog. ModelDetail watches uid and workspace + // separately, and this is the arm where a stale answer would overwrite the + // newer workspace rather than reach for another account's. + it('drops a pending switch when only the workspace changes', async () => { + signIn() + const cancel = vi.fn() + reportWorkshopRun(cancel) + onTestFinished(() => reportWorkshopRun(undefined)) + vi.stubGlobal( + 'fetch', + vi + .fn() + .mockResolvedValue( + new Response(JSON.stringify(listing), { status: 200 }) + ) + ) + const user = userEvent.setup() + render(HeaderAccount) + + await openSwitcher(user) + await user.click(await screen.findByTestId('account-workspace-team-1')) + expect(await screen.findByTestId('run-leave-dialog')).toBeTruthy() + + const moved = { + token: 'jwt', + uid: 'user-1', + workspace: { id: 'team-2', name: 'Other team', type: 'team' as const }, + role: 'owner' + } + h.session!.value = moved + reportWorkshopRun(undefined) + + await waitFor(() => + expect(screen.queryByTestId('run-leave-dialog')).toBeNull() + ) + expect(h.remint).not.toHaveBeenCalled() + expect(cancel).not.toHaveBeenCalled() + expect(h.session!.value).toEqual(moved) + }) + it('switches by reminting for the picked workspace', async () => { signIn() vi.stubGlobal( diff --git a/apps/website/src/components/workshop/HeaderAccount.vue b/apps/website/src/components/workshop/HeaderAccount.vue index edf0cf49596..7e76e2f6564 100644 --- a/apps/website/src/components/workshop/HeaderAccount.vue +++ b/apps/website/src/components/workshop/HeaderAccount.vue @@ -165,16 +165,54 @@ async function restorePreviousWorkspace( // A run belongs to the workspace that is paying for it, so switching ends it. // The playground guards every way off the page; this is the way off the // workspace, and it has to ask the same question before it takes the credits. -const switchPending = ref() +type PendingWorkspaceSwitch = { + targetWorkspaceId: string + sourceUid: string + sourceWorkspaceId: string +} + +const switchPending = ref() + +function pendingSwitchStillOwnsSession( + pending: PendingWorkspaceSwitch +): boolean { + const current = session.value + return ( + current?.uid === pending.sourceUid && + current.workspace.id === pending.sourceWorkspaceId + ) +} function confirmSwitch() { - const workspaceId = switchPending.value + const pending = switchPending.value switchPending.value = undefined - if (!workspaceId) return + if (!pending || !pendingSwitchStillOwnsSession(pending)) return cancelWorkshopRun() - void switchWorkspace(workspaceId, { runAlreadyCancelled: true }) + void switchWorkspace(pending.targetWorkspaceId, { + runAlreadyCancelled: true + }) } +// The question only exists because a run would be thrown away. A run that ends +// on its own answers it: there is nothing left to cancel, so the switch the +// reader already asked for goes through and the dialog closes with it. +watch( + [ + workshopRunInFlight, + () => session.value?.uid, + () => session.value?.workspace.id + ], + ([inFlight]) => { + const pending = switchPending.value + if (!pending) return + if (inFlight && pendingSwitchStillOwnsSession(pending)) return + + const shouldContinue = pendingSwitchStillOwnsSession(pending) + switchPending.value = undefined + if (shouldContinue) void switchWorkspace(pending.targetWorkspaceId) + } +) + async function applySwitch( workspaceId: string, previous: ActiveWorkshopSession @@ -215,7 +253,11 @@ async function switchWorkspace( return } if (workshopRunInFlight.value && !options?.runAlreadyCancelled) { - switchPending.value = workspaceId + switchPending.value = { + targetWorkspaceId: workspaceId, + sourceUid: previous.uid, + sourceWorkspaceId: previous.workspace.id + } return } await applySwitch(workspaceId, previous)