|
| 1 | +import { expect } from '@playwright/test' |
| 2 | +import { execaCommand } from 'execa' |
| 3 | +import { |
| 4 | + createE2EFixture, |
| 5 | + createSite, |
| 6 | + deleteSite, |
| 7 | + getBuildFixtureVariantCommand, |
| 8 | + publishDeploy, |
| 9 | +} from '../utils/create-e2e-fixture.js' |
| 10 | +import { test as baseTest } from '../utils/playwright-helpers.js' |
| 11 | + |
| 12 | +type ExtendedFixtures = { |
| 13 | + skewProtection: { |
| 14 | + siteId: string |
| 15 | + url: string |
| 16 | + deploy1: Awaited<ReturnType<typeof createE2EFixture>> |
| 17 | + deploy2: Awaited<ReturnType<typeof createE2EFixture>> |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +const test = baseTest.extend< |
| 22 | + { prepareSkewProtectionScenario: <T>(callback: () => T) => Promise<T> }, |
| 23 | + ExtendedFixtures |
| 24 | +>({ |
| 25 | + prepareSkewProtectionScenario: async ({ skewProtection }, use) => { |
| 26 | + // first we will publish deploy1 |
| 27 | + // then we call arbitrary callback to allow tests to load page using deploy1 |
| 28 | + // and after that we will publish deploy2 |
| 29 | + |
| 30 | + const fixture = async <T>(callback: () => T) => { |
| 31 | + await publishDeploy(skewProtection.siteId, skewProtection.deploy1.deployID) |
| 32 | + // poll to ensure deploy was restored before continuing |
| 33 | + while (true) { |
| 34 | + const response = await fetch(`${skewProtection.url}/variant.txt`) |
| 35 | + const text = await response.text() |
| 36 | + if (text.startsWith('A')) { |
| 37 | + break |
| 38 | + } |
| 39 | + await new Promise((resolve) => setTimeout(resolve, 1000)) |
| 40 | + } |
| 41 | + console.log('Deploy 1 published') |
| 42 | + |
| 43 | + const result = await callback() |
| 44 | + await new Promise((resolve) => setTimeout(resolve, 10_000)) |
| 45 | + await publishDeploy(skewProtection.siteId, skewProtection.deploy2.deployID) |
| 46 | + |
| 47 | + // poll to ensure deploy was restored before continuing |
| 48 | + while (true) { |
| 49 | + const response = await fetch(`${skewProtection.url}/variant.txt`) |
| 50 | + const text = await response.text() |
| 51 | + if (text.startsWith('B')) { |
| 52 | + break |
| 53 | + } |
| 54 | + await new Promise((resolve) => setTimeout(resolve, 1000)) |
| 55 | + } |
| 56 | + console.log('Deploy 2 published') |
| 57 | + |
| 58 | + return result |
| 59 | + } |
| 60 | + |
| 61 | + await use(fixture) |
| 62 | + }, |
| 63 | + skewProtection: [ |
| 64 | + async ({}, use) => { |
| 65 | + // await use({ |
| 66 | + // url: 'https://next-skew-tests-1758183689147.netlify.app', |
| 67 | + // siteId: 'cccd0ac4-fecd-4240-9263-01df2e613dd0', |
| 68 | + // deploy1: { |
| 69 | + // deployID: '68cbc1121ed77e68c4f85a17', |
| 70 | + // url: 'https://68cbc1121ed77e68c4f85a17--cccd0ac4-fecd-4240-9263-01df2e613dd0.netlify.app', |
| 71 | + // }, |
| 72 | + // deploy2: { |
| 73 | + // deployID: '68cbc11b52952577e923929a', |
| 74 | + // url: 'https://68cbc11b52952577e923929a--cccd0ac4-fecd-4240-9263-01df2e613dd0.netlify.app', |
| 75 | + // }, |
| 76 | + // }) |
| 77 | + |
| 78 | + // return |
| 79 | + |
| 80 | + const { siteId, url } = await createSite({ |
| 81 | + name: `next-skew-tests-${Date.now()}`, |
| 82 | + }) |
| 83 | + |
| 84 | + let onBuildStart: () => void = () => {} |
| 85 | + const waitForBuildStart = new Promise<void>((resolve) => { |
| 86 | + onBuildStart = () => { |
| 87 | + resolve() |
| 88 | + } |
| 89 | + }) |
| 90 | + |
| 91 | + const deploy1Promise = createE2EFixture('skew-protection', { |
| 92 | + siteId, |
| 93 | + useBuildbot: true, |
| 94 | + onBuildStart, |
| 95 | + env: { |
| 96 | + // NETLIFY_NEXT_PLUGIN_SKEW_PROTECTION: 'true', |
| 97 | + }, |
| 98 | + }) |
| 99 | + |
| 100 | + // we don't have to wait for deploy1 to finish completely before starting deploy2, but we do have to wait a little bit |
| 101 | + // to at least when build is scheduled, as otherwise whole deploy might be skipped |
| 102 | + await waitForBuildStart |
| 103 | + |
| 104 | + const deploy2Promise = createE2EFixture('skew-protection', { |
| 105 | + siteId, |
| 106 | + useBuildbot: true, |
| 107 | + env: { |
| 108 | + // NETLIFY_NEXT_PLUGIN_SKEW_PROTECTION: 'true', |
| 109 | + }, |
| 110 | + onPreDeploy: async (fixtureRoot) => { |
| 111 | + await execaCommand( |
| 112 | + `${getBuildFixtureVariantCommand('variant-b')} --apply-file-changes-only`, |
| 113 | + { |
| 114 | + cwd: fixtureRoot, |
| 115 | + }, |
| 116 | + ) |
| 117 | + }, |
| 118 | + }) |
| 119 | + |
| 120 | + const [deploy1, deploy2] = await Promise.all([deploy1Promise, deploy2Promise]) |
| 121 | + |
| 122 | + const fixture = { |
| 123 | + url, |
| 124 | + siteId, |
| 125 | + deploy1, |
| 126 | + deploy2, |
| 127 | + |
| 128 | + cleanup: async () => { |
| 129 | + if (process.env.E2E_PERSIST) { |
| 130 | + console.log( |
| 131 | + `💾 Fixture and deployed site have been persisted. To clean up automatically, run tests without the 'E2E_PERSIST' environment variable.`, |
| 132 | + ) |
| 133 | + |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + await deploy1.cleanup() |
| 138 | + await deploy2.cleanup() |
| 139 | + await deleteSite(siteId) |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + // for local iteration - this will print out snippet to allow to reuse previously deployed setup |
| 144 | + // paste this at the top of `skewProtection` fixture function and this will avoid having to wait for redeploys |
| 145 | + // keep in mind that if fixture itself require changes, you will have to redeploy |
| 146 | + console.log(`await use(${JSON.stringify(fixture, null, 2)})\n\nreturn`) |
| 147 | + await use(fixture) |
| 148 | + |
| 149 | + await fixture.cleanup() |
| 150 | + }, |
| 151 | + { |
| 152 | + scope: 'worker', |
| 153 | + }, |
| 154 | + ], |
| 155 | +}) |
| 156 | + |
| 157 | +test.describe('Skew Protection', () => { |
| 158 | + test.describe('App Router', () => { |
| 159 | + test('should scope server actions to initial deploy', async ({ |
| 160 | + page, |
| 161 | + skewProtection, |
| 162 | + prepareSkewProtectionScenario, |
| 163 | + }) => { |
| 164 | + await prepareSkewProtectionScenario(() => page.goto(`${skewProtection.url}/app-router`)) |
| 165 | + |
| 166 | + await page.getByTestId('server-action-button').click() |
| 167 | + |
| 168 | + // if skew protection does not work, this will be either "B" (currently published deploy) |
| 169 | + // or error about not finding server action - example of such error: |
| 170 | + // "Error: Server Action "00a130b1673301d79679b22abb06a62c3125376d79" was not found on the server. |
| 171 | + // Read more: https://nextjs.org/docs/messages/failed-to-find-server-action" |
| 172 | + await expect(page.getByTestId('server-action-result')).toHaveText(`"A"`) |
| 173 | + }) |
| 174 | + }) |
| 175 | +}) |
0 commit comments