|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { |
| 6 | + type DeployButtonStatus, |
| 7 | + resolveDeployButtonStatus, |
| 8 | +} from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/deploy/hooks/use-deploy-button-status' |
| 9 | + |
| 10 | +type Input = Parameters<typeof resolveDeployButtonStatus>[0] |
| 11 | + |
| 12 | +const base: Input = { |
| 13 | + workflowId: 'wf-1', |
| 14 | + isDeployed: false, |
| 15 | + isAwaitingFirstDeployedState: false, |
| 16 | + clientChangeDetected: false, |
| 17 | + hasDeployedState: false, |
| 18 | + serverNeedsRedeployment: undefined, |
| 19 | +} |
| 20 | + |
| 21 | +/** Replays a render sequence and returns the labels actually committed, deduped. */ |
| 22 | +function committed(sequence: Array<Partial<Input>>): DeployButtonStatus[] { |
| 23 | + const seen: DeployButtonStatus[] = [] |
| 24 | + for (const step of sequence) { |
| 25 | + const status = resolveDeployButtonStatus({ ...base, ...step }) |
| 26 | + if (seen[seen.length - 1] !== status) seen.push(status) |
| 27 | + } |
| 28 | + return seen |
| 29 | +} |
| 30 | + |
| 31 | +describe('resolveDeployButtonStatus', () => { |
| 32 | + /** |
| 33 | + * The regression this exists for. The old label read `changeDetected`, which |
| 34 | + * is forced false while the deployed snapshot loads, so a changed workflow |
| 35 | + * rendered "Live" on the way to "Update". |
| 36 | + */ |
| 37 | + it('never passes through live when loading a workflow that has changes', () => { |
| 38 | + const statuses = committed([ |
| 39 | + // 1. Nothing loaded. |
| 40 | + {}, |
| 41 | + // 2. deploymentInfo lands — isDeployed and needsRedeployment arrive together. |
| 42 | + { isDeployed: true, serverNeedsRedeployment: true, isAwaitingFirstDeployedState: true }, |
| 43 | + // 3. The deployed snapshot lands; the client diff agrees. |
| 44 | + { |
| 45 | + isDeployed: true, |
| 46 | + serverNeedsRedeployment: true, |
| 47 | + hasDeployedState: true, |
| 48 | + clientChangeDetected: true, |
| 49 | + }, |
| 50 | + ]) |
| 51 | + |
| 52 | + expect(statuses).toEqual(['undeployed', 'changed']) |
| 53 | + expect(statuses).not.toContain('live') |
| 54 | + }) |
| 55 | + |
| 56 | + it('settles straight to live for a deployed workflow with no changes', () => { |
| 57 | + const statuses = committed([ |
| 58 | + {}, |
| 59 | + { isDeployed: true, serverNeedsRedeployment: false, isAwaitingFirstDeployedState: true }, |
| 60 | + { isDeployed: true, serverNeedsRedeployment: false, hasDeployedState: true }, |
| 61 | + ]) |
| 62 | + |
| 63 | + expect(statuses).toEqual(['undeployed', 'live']) |
| 64 | + expect(statuses).not.toContain('changed') |
| 65 | + }) |
| 66 | + |
| 67 | + /** |
| 68 | + * `refetchOnWindowFocus` is on for both queries, so this fires on every focus. |
| 69 | + * A refetch keeps the cached snapshot, so the answer must not move. |
| 70 | + */ |
| 71 | + it('holds its answer across a background refetch', () => { |
| 72 | + const settled: Partial<Input> = { |
| 73 | + isDeployed: true, |
| 74 | + serverNeedsRedeployment: true, |
| 75 | + hasDeployedState: true, |
| 76 | + clientChangeDetected: true, |
| 77 | + } |
| 78 | + |
| 79 | + const statuses = committed([ |
| 80 | + settled, |
| 81 | + // Refetching: data is still cached, so `isAwaitingFirstDeployedState` stays false. |
| 82 | + settled, |
| 83 | + settled, |
| 84 | + ]) |
| 85 | + |
| 86 | + expect(statuses).toEqual(['changed']) |
| 87 | + }) |
| 88 | + |
| 89 | + it('prefers the client diff over the server seed once a snapshot exists', () => { |
| 90 | + // Unsaved edits: the server still describes the persisted draft. |
| 91 | + const status = resolveDeployButtonStatus({ |
| 92 | + ...base, |
| 93 | + isDeployed: true, |
| 94 | + serverNeedsRedeployment: false, |
| 95 | + hasDeployedState: true, |
| 96 | + clientChangeDetected: true, |
| 97 | + }) |
| 98 | + |
| 99 | + expect(status).toBe('changed') |
| 100 | + }) |
| 101 | + |
| 102 | + it('reports undeployed without a workflow', () => { |
| 103 | + expect(resolveDeployButtonStatus({ ...base, workflowId: null })).toBe('undeployed') |
| 104 | + }) |
| 105 | + |
| 106 | + it('falls back to unknown only when deployed with no verdict from either side', () => { |
| 107 | + const status = resolveDeployButtonStatus({ |
| 108 | + ...base, |
| 109 | + isDeployed: true, |
| 110 | + isAwaitingFirstDeployedState: true, |
| 111 | + serverNeedsRedeployment: undefined, |
| 112 | + }) |
| 113 | + |
| 114 | + expect(status).toBe('unknown') |
| 115 | + }) |
| 116 | +}) |
0 commit comments