|
1 | 1 | import fs from 'node:fs/promises' |
2 | 2 | import path from 'node:path' |
| 3 | +import { styleBuilder } from '@purestack/ts-style' |
3 | 4 |
|
4 | 5 | import { disableLogger, getLogger, type Logger } from 'logpot' |
5 | | -import { afterAll, beforeAll, describe, expect, it } from 'vitest' |
| 6 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' |
6 | 7 | import { resolveSiteConfig } from '../../config/config' |
7 | 8 | import { parseFrontmatterSource } from '../../frontmatter/frontmatter' |
8 | 9 | import type { PureStackPlugin } from '../../plugins/plugin' |
@@ -59,6 +60,161 @@ describe('incremental builder', () => { |
59 | 60 | await logger?.close() |
60 | 61 | }) |
61 | 62 |
|
| 63 | + it('keeps style changes during CSS generation pending for the next request', async () => { |
| 64 | + await withTempDir(async (base) => { |
| 65 | + const contentDir = path.join(base, 'content') |
| 66 | + const outDir = path.join(base, 'out') |
| 67 | + await fs.mkdir(contentDir) |
| 68 | + await fs.writeFile(path.join(contentDir, 'index.md'), '# Home') |
| 69 | + let writes = 0 |
| 70 | + const builder = await createIncrementalBuilder({ |
| 71 | + siteConfig: { rootDir: base, contentDir, outDir }, |
| 72 | + options: { |
| 73 | + plugins: [ |
| 74 | + { |
| 75 | + name: 'count-css', |
| 76 | + hooks: { |
| 77 | + onStylesWritten() { |
| 78 | + writes += 1 |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + ], |
| 83 | + }, |
| 84 | + }) |
| 85 | + await builder.prepareForRequests() |
| 86 | + await builder.renderByUrlPath('/') |
| 87 | + const render = styleBuilder.render.bind(styleBuilder) |
| 88 | + let changed = false |
| 89 | + const spy = vi |
| 90 | + .spyOn(styleBuilder, 'render') |
| 91 | + .mockImplementation(async (theme, pretty) => { |
| 92 | + const css = await render(theme, pretty) |
| 93 | + if (!changed) { |
| 94 | + changed = true |
| 95 | + styleBuilder.select('.concurrent-style', 'light').color('red') |
| 96 | + } |
| 97 | + return css |
| 98 | + }) |
| 99 | + try { |
| 100 | + await builder.preparePageAssets() |
| 101 | + expect(writes).toBe(1) |
| 102 | + await builder.preparePageAssets() |
| 103 | + expect(writes).toBe(2) |
| 104 | + expect( |
| 105 | + await fs.readFile(path.join(outDir, 'assets', 'site.css'), 'utf8'), |
| 106 | + ).toContain('.concurrent-style') |
| 107 | + await builder.preparePageAssets() |
| 108 | + expect(writes).toBe(2) |
| 109 | + } finally { |
| 110 | + spy.mockRestore() |
| 111 | + } |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + it.each(['none', 'hybrid'] as const)( |
| 116 | + 'shares discovery across watcher bursts with %s navigation', |
| 117 | + async (mode) => { |
| 118 | + await withTempDir(async (base) => { |
| 119 | + const contentDir = path.join(base, 'content') |
| 120 | + const outDir = path.join(base, 'out') |
| 121 | + await fs.mkdir(contentDir) |
| 122 | + const files = Array.from({ length: 10 }, (_, index) => |
| 123 | + path.join(contentDir, `page${index}.md`), |
| 124 | + ) |
| 125 | + for (const file of files) await fs.writeFile(file, '# Original') |
| 126 | + let discoveries = 0 |
| 127 | + const builder = await createIncrementalBuilder({ |
| 128 | + siteConfig: { |
| 129 | + rootDir: base, |
| 130 | + contentDir, |
| 131 | + outDir, |
| 132 | + navigation: { mode }, |
| 133 | + }, |
| 134 | + options: { |
| 135 | + plugins: [ |
| 136 | + { |
| 137 | + name: 'count-discoveries', |
| 138 | + pages() { |
| 139 | + discoveries += 1 |
| 140 | + return [] |
| 141 | + }, |
| 142 | + }, |
| 143 | + ], |
| 144 | + }, |
| 145 | + }) |
| 146 | + await builder.prepareForRequests() |
| 147 | + await builder.renderByUrlPath('/page0/') |
| 148 | + discoveries = 0 |
| 149 | + expect(await builder.applyChanges([...files, files[0]])).toHaveLength( |
| 150 | + 10, |
| 151 | + ) |
| 152 | + expect(discoveries).toBe(1) |
| 153 | + |
| 154 | + await fs.writeFile(files[0], '# Updated') |
| 155 | + await fs.rm(files[1]) |
| 156 | + const added = path.join(contentDir, 'added.md') |
| 157 | + await fs.writeFile(added, '# Added') |
| 158 | + await builder.applyChanges([files[0], files[1], added]) |
| 159 | + expect(discoveries).toBe(2) |
| 160 | + expect(await builder.renderByUrlPath('/page0/')).toBe(true) |
| 161 | + expect( |
| 162 | + await fs.readFile(path.join(outDir, 'page0', 'index.html'), 'utf8'), |
| 163 | + ).toContain('Updated') |
| 164 | + expect(await builder.renderByUrlPath('/added/')).toBe(true) |
| 165 | + expect(await builder.renderByUrlPath('/page1/')).toBe(false) |
| 166 | + }) |
| 167 | + }, |
| 168 | + ) |
| 169 | + |
| 170 | + it('stops a change batch on abort and clears its discovery cache after failure', async () => { |
| 171 | + await withTempDir(async (base) => { |
| 172 | + const contentDir = path.join(base, 'content') |
| 173 | + await fs.mkdir(contentDir) |
| 174 | + const first = path.join(contentDir, 'first.md') |
| 175 | + const second = path.join(contentDir, 'second.md') |
| 176 | + await fs.writeFile(first, '# First') |
| 177 | + await fs.writeFile(second, '# Second') |
| 178 | + const controller = new AbortController() |
| 179 | + let abort = false |
| 180 | + let fail = false |
| 181 | + const builder = await createIncrementalBuilder({ |
| 182 | + siteConfig: { |
| 183 | + rootDir: base, |
| 184 | + contentDir, |
| 185 | + outDir: path.join(base, 'out'), |
| 186 | + }, |
| 187 | + options: { |
| 188 | + plugins: [ |
| 189 | + { |
| 190 | + name: 'control-discovery', |
| 191 | + pages() { |
| 192 | + if (fail) throw new Error('discovery failed') |
| 193 | + if (abort) controller.abort() |
| 194 | + return [] |
| 195 | + }, |
| 196 | + }, |
| 197 | + ], |
| 198 | + }, |
| 199 | + }) |
| 200 | + await builder.prepareForRequests() |
| 201 | + abort = true |
| 202 | + expect( |
| 203 | + await builder.applyChanges([first, second], controller.signal), |
| 204 | + ).toHaveLength(1) |
| 205 | + expect(await builder.applyChanges([second], controller.signal)).toEqual( |
| 206 | + [], |
| 207 | + ) |
| 208 | + abort = false |
| 209 | + fail = true |
| 210 | + await expect(builder.applyChanges([first])).rejects.toThrow( |
| 211 | + 'discovery failed', |
| 212 | + ) |
| 213 | + fail = false |
| 214 | + expect(await builder.applyChanges([first, second])).toHaveLength(2) |
| 215 | + }) |
| 216 | + }) |
| 217 | + |
62 | 218 | it('ignores directory notifications and refreshes removed subtrees without deleting directories as files', async () => { |
63 | 219 | await withTempDir(async (base) => { |
64 | 220 | const contentDir = path.join(base, 'content') |
|
0 commit comments