|
1 | 1 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { |
| 3 | + buildSelectorContextFromValues, |
| 4 | + getSelectorContextSubBlocks, |
| 5 | +} from '@/lib/selectors/context' |
2 | 6 | import { createSelectorProtectedValues } from '@/lib/selectors/server/protected-values' |
3 | 7 | import { planetScaleSelectorAttachments } from '@/lib/selectors/server/providers/planetscale' |
4 | 8 | import type { ExecuteServerSelectorArgs } from '@/lib/selectors/server/types' |
| 9 | +import { getDependsOnFields } from '@/lib/workflows/subblocks/dependencies' |
| 10 | +import { buildCanonicalIndexForSurface } from '@/lib/workflows/subblocks/visibility' |
| 11 | +import { PlanetScaleBlock } from '@/blocks/blocks/planetscale' |
5 | 12 |
|
6 | 13 | function args(overrides: Partial<ExecuteServerSelectorArgs> = {}): ExecuteServerSelectorArgs { |
7 | 14 | return { |
@@ -110,13 +117,69 @@ describe('PlanetScale server selectors', () => { |
110 | 117 | ).resolves.toEqual({ kind: 'list', items: [option] }) |
111 | 118 | }) |
112 | 119 |
|
| 120 | + it.each([ |
| 121 | + ['get_backup', 'branchSelector'], |
| 122 | + ['create_branch', 'parentBranchSelector'], |
| 123 | + ])( |
| 124 | + 'loads backups from the selected branch on the %s block surface', |
| 125 | + async (operation, branchField) => { |
| 126 | + fetchMock.mockResolvedValueOnce(page([{ id: 'backup-id', name: 'Before migration' }])) |
| 127 | + const values = { |
| 128 | + serviceTokenId: 'test-id', |
| 129 | + serviceToken: 'test-secret', |
| 130 | + organization: 'example', |
| 131 | + databaseSelector: 'test-db', |
| 132 | + operation, |
| 133 | + [branchField]: 'development', |
| 134 | + [branchField === 'branchSelector' ? 'parentBranchSelector' : 'branchSelector']: |
| 135 | + 'stale-hidden', |
| 136 | + } |
| 137 | + const backupPicker = PlanetScaleBlock.subBlocks.find( |
| 138 | + (field) => field.id === 'backupIdSelector' |
| 139 | + )! |
| 140 | + const context = buildSelectorContextFromValues({ |
| 141 | + selectorKey: 'planetscale.backups', |
| 142 | + contextConfigs: getSelectorContextSubBlocks(PlanetScaleBlock.subBlocks, values), |
| 143 | + values, |
| 144 | + dependsOn: getDependsOnFields(backupPicker.dependsOn), |
| 145 | + canonicalIndex: buildCanonicalIndexForSurface(PlanetScaleBlock.subBlocks, false), |
| 146 | + }) |
| 147 | + await expect( |
| 148 | + planetScaleSelectorAttachments['planetscale.backups'].execute( |
| 149 | + args({ selectorKey: 'planetscale.backups', context }) |
| 150 | + ) |
| 151 | + ).resolves.toEqual({ kind: 'list', items: [{ id: 'backup-id', label: 'Before migration' }] }) |
| 152 | + expect(fetchMock.mock.calls.map(([url]) => new URL(String(url)).pathname)).toEqual([ |
| 153 | + '/v1/organizations/example/databases/test-db/branches/development/backups', |
| 154 | + ]) |
| 155 | + } |
| 156 | + ) |
| 157 | + |
113 | 158 | it.each([undefined, ''])( |
114 | 159 | 'discovers backups from the default branch when parent is %s', |
115 | 160 | async (branch) => { |
116 | 161 | fetchMock.mockResolvedValueOnce(Response.json({ default_branch: 'production' })) |
117 | 162 | fetchMock.mockResolvedValueOnce(page([{ id: 'backup-id', name: 'Before migration' }])) |
118 | | - const input = args({ selectorKey: 'planetscale.backups' }) |
119 | | - input.context.branch = branch |
| 163 | + const values = { |
| 164 | + serviceTokenId: 'test-id', |
| 165 | + serviceToken: 'test-secret', |
| 166 | + organization: 'example', |
| 167 | + databaseSelector: 'test-db', |
| 168 | + operation: 'create_branch', |
| 169 | + branchSelector: 'stale-hidden', |
| 170 | + parentBranchSelector: branch, |
| 171 | + } |
| 172 | + const backupPicker = PlanetScaleBlock.subBlocks.find( |
| 173 | + (field) => field.id === 'backupIdSelector' |
| 174 | + )! |
| 175 | + const context = buildSelectorContextFromValues({ |
| 176 | + selectorKey: 'planetscale.backups', |
| 177 | + contextConfigs: getSelectorContextSubBlocks(PlanetScaleBlock.subBlocks, values), |
| 178 | + values, |
| 179 | + dependsOn: getDependsOnFields(backupPicker.dependsOn), |
| 180 | + canonicalIndex: buildCanonicalIndexForSurface(PlanetScaleBlock.subBlocks, false), |
| 181 | + }) |
| 182 | + const input = args({ selectorKey: 'planetscale.backups', context }) |
120 | 183 | await expect( |
121 | 184 | planetScaleSelectorAttachments['planetscale.backups'].execute(input) |
122 | 185 | ).resolves.toEqual({ |
|
0 commit comments