|
| 1 | +/** |
| 2 | + * The example's golden path, end to end against a mocked deploy surface: |
| 3 | + * build → deploy to a `play` preview → publish → edit one page → redeploy → |
| 4 | + * roll back. |
| 5 | + */ |
| 6 | + |
| 7 | +import { deployNames, deploySite, publishCommit } from '@constructive-io/site-deploy'; |
| 8 | +import { mkdtemp, readFile, rm, writeFile } from 'fs/promises'; |
| 9 | +import { tmpdir } from 'os'; |
| 10 | +import { join } from 'path'; |
| 11 | + |
| 12 | +import { buildSite } from '../src/ssg'; |
| 13 | +import { createMockServer } from './mock-server'; |
| 14 | + |
| 15 | +const CONTENT = join(__dirname, '..', 'content'); |
| 16 | +const names = deployNames(); |
| 17 | + |
| 18 | +let work: string; |
| 19 | +let content: string; |
| 20 | +let dist: string; |
| 21 | + |
| 22 | +beforeEach(async () => { |
| 23 | + work = await mkdtemp(join(tmpdir(), 'ssg-deploy-')); |
| 24 | + content = join(work, 'content'); |
| 25 | + dist = join(work, 'dist'); |
| 26 | + await copyContent(CONTENT, content); |
| 27 | +}); |
| 28 | + |
| 29 | +afterEach(async () => { |
| 30 | + await rm(work, { recursive: true, force: true }); |
| 31 | +}); |
| 32 | + |
| 33 | +async function copyContent(from: string, to: string): Promise<void> { |
| 34 | + const { cp } = await import('fs/promises'); |
| 35 | + await cp(from, to, { recursive: true }); |
| 36 | +} |
| 37 | + |
| 38 | +const deployOptions = (server: ReturnType<typeof createMockServer>) => ({ |
| 39 | + api: server.api, |
| 40 | + putObject: server.putObject, |
| 41 | + siteId: 'site-1', |
| 42 | + databaseId: 'db-1', |
| 43 | + bucketKey: 'site-example', |
| 44 | + source: dist, |
| 45 | +}); |
| 46 | + |
| 47 | +test('builds every page, the stylesheet and a 404', async () => { |
| 48 | + const files = await buildSite(content, dist); |
| 49 | + expect(files).toEqual([ |
| 50 | + '404.html', |
| 51 | + 'about.html', |
| 52 | + 'assets/site.css', |
| 53 | + 'index.html', |
| 54 | + 'previews.html', |
| 55 | + ]); |
| 56 | + const index = await readFile(join(dist, 'index.html'), 'utf8'); |
| 57 | + expect(index).toContain('<title>Constructive SSG example</title>'); |
| 58 | + // The nav links every page, with index at the site root. |
| 59 | + expect(index).toContain('href="/"'); |
| 60 | + expect(index).toContain('href="/previews.html"'); |
| 61 | +}); |
| 62 | + |
| 63 | +test('deploys the build to a play preview without touching production', async () => { |
| 64 | + const server = createMockServer(); |
| 65 | + await buildSite(content, dist); |
| 66 | + |
| 67 | + const result = await deploySite({ |
| 68 | + ...deployOptions(server), |
| 69 | + preview: 'play', |
| 70 | + previewApex: 'preview.example.com', |
| 71 | + }); |
| 72 | + |
| 73 | + expect(result.files).toBe(5); |
| 74 | + expect(result.uploaded).toBe(5); |
| 75 | + expect(result.skipped).toBe(0); |
| 76 | + expect(result.commitId).toBe('commit-1'); |
| 77 | + expect(result.previewUrl).toBe('https://play--example.preview.example.com'); |
| 78 | + expect(server.previewRefs).toEqual({ play: 'commit-1' }); |
| 79 | + // Production is still unpublished: a preview deploy moves no pointer. |
| 80 | + expect(result.published).toBe(false); |
| 81 | + expect(server.activeCommitId).toBeNull(); |
| 82 | + |
| 83 | + // Bytes are addressed by content, and the manifest carries served types. |
| 84 | + expect(server.storedKeys).toHaveLength(5); |
| 85 | + for (const key of server.storedKeys) expect(key).toMatch(/^cas\/sha256\/[0-9a-f]{64}$/); |
| 86 | + expect(result.manifest.files['assets/site.css'].content_type).toBe('text/css; charset=utf-8'); |
| 87 | + expect(result.manifest.files['index.html'].content_type).toBe('text/html; charset=utf-8'); |
| 88 | +}); |
| 89 | + |
| 90 | +test('publishing the previewed commit is the same pointer move', async () => { |
| 91 | + const server = createMockServer(); |
| 92 | + await buildSite(content, dist); |
| 93 | + |
| 94 | + const first = await deploySite({ ...deployOptions(server), preview: 'play' }); |
| 95 | + expect(server.activeCommitId).toBeNull(); |
| 96 | + |
| 97 | + await publishCommit(server.api, names, 'site-1', first.commitId); |
| 98 | + expect(server.activeCommitId).toBe(first.commitId); |
| 99 | +}); |
| 100 | + |
| 101 | +test('editing one page re-uploads one file and leaves production behind', async () => { |
| 102 | + const server = createMockServer(); |
| 103 | + await buildSite(content, dist); |
| 104 | + const first = await deploySite({ ...deployOptions(server), publish: true }); |
| 105 | + expect(server.activeCommitId).toBe(first.commitId); |
| 106 | + |
| 107 | + await writeFile(join(content, 'about.html'), '# About\n<p>Now with a changelog.</p>\n'); |
| 108 | + await buildSite(content, dist); |
| 109 | + |
| 110 | + const second = await deploySite({ ...deployOptions(server), preview: 'play' }); |
| 111 | + |
| 112 | + expect(second.commitId).not.toBe(first.commitId); |
| 113 | + expect(second.uploaded).toBe(1); |
| 114 | + expect(second.skipped).toBe(4); |
| 115 | + expect(second.manifest.files['about.html'].hash).not.toBe( |
| 116 | + first.manifest.files['about.html'].hash, |
| 117 | + ); |
| 118 | + expect(second.manifest.files['index.html'].hash).toBe(first.manifest.files['index.html'].hash); |
| 119 | + // The preview moved; production stayed on the first release. |
| 120 | + expect(server.previewRefs.play).toBe(second.commitId); |
| 121 | + expect(server.activeCommitId).toBe(first.commitId); |
| 122 | + |
| 123 | + // Publishing then rolling back is one mutation each way. |
| 124 | + await publishCommit(server.api, names, 'site-1', second.commitId); |
| 125 | + expect(server.activeCommitId).toBe(second.commitId); |
| 126 | + await publishCommit(server.api, names, 'site-1', first.commitId); |
| 127 | + expect(server.activeCommitId).toBe(first.commitId); |
| 128 | +}); |
| 129 | + |
| 130 | +test('redeploying an unchanged build writes no new release', async () => { |
| 131 | + const server = createMockServer(); |
| 132 | + await buildSite(content, dist); |
| 133 | + const first = await deploySite({ ...deployOptions(server), publish: true }); |
| 134 | + |
| 135 | + await buildSite(content, dist); |
| 136 | + const again = await deploySite({ ...deployOptions(server), skipIfUnchanged: true }); |
| 137 | + |
| 138 | + expect(again.unchanged).toBe(true); |
| 139 | + expect(again.commitId).toBe(first.commitId); |
| 140 | + expect(again.uploaded).toBe(0); |
| 141 | +}); |
0 commit comments