summaryrefslogtreecommitdiff
path: root/packages/astro/test/experimental-content-collections-cache-invalidation.test.js
blob: 5ec688a91ba1aa2dc4decff63367af471cfd69b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
import fs from 'node:fs';
import { copyFiles } from '../dist/core/build/static-build.js';

describe('Experimental Content Collections cache - invalidation', () => {
	class CacheBackup {
		constructor(root, relCacheDir) {
			this.root = new URL(root, import.meta.url);
			this.cacheDir = new URL(relCacheDir, this.root);
			this.tmpDir = new URL(`./tmp` + relCacheDir.slice(1), this.root);
		}
		backup() {
			this.rmTmp();
			copyFiles(this.cacheDir, this.tmpDir);
		}
		restore() {
			fs.rmSync(this.cacheDir, { recursive: true });
			copyFiles(this.tmpDir, this.cacheDir);
		}
		rmTmp() {
			fs.rmSync(this.tmpDir, { force: true, recursive: true });
		}
	}

	class ManifestTestPlugin {
		used = false;
		plugin() {
			return {
				name: '@test/manifest-used',
				hooks: {
					'astro:build:done': ({ cacheManifest }) => {
						this.used = cacheManifest;
					}
				}
			}
		}
	}

	describe('manifest version', () => {
		let fixture, backup,
		/** @type {ManifestTestPlugin} */
		testPlugin;
		before(async () => {
			testPlugin = new ManifestTestPlugin();
			fixture = await loadFixture({
				root: './fixtures/content-collections-cache-invalidation/',
				cacheDir: './cache/version-mismatch/',
				experimental: { contentCollectionCache: true },
				integrations: [
					testPlugin.plugin()
				]
			});
			backup = new CacheBackup('./fixtures/content-collections-cache-invalidation/', './cache/version-mismatch/');
			backup.backup();
			await fixture.build();
		});

		after(async () => {
			backup.restore();
			//await fixture.clean();
		});

		it('Manifest was not used', () => {
			assert.equal(testPlugin.used, false, 'manifest not used because of version mismatch');
		});
	});

	describe('lockfiles', () => {
		let fixture, backup,
		/** @type {ManifestTestPlugin} */
		testPlugin;
		before(async () => {
			testPlugin = new ManifestTestPlugin();
			fixture = await loadFixture({
				root: './fixtures/content-collections-cache-invalidation/',
				cacheDir: './cache/lockfile-mismatch/',
				experimental: { contentCollectionCache: true },
				integrations: [
					testPlugin.plugin()
				]
			});
			backup = new CacheBackup('./fixtures/content-collections-cache-invalidation/', './cache/lockfile-mismatch/');
			backup.backup();
			await fixture.build();
		});

		after(async () => {
			backup.restore();
			//await fixture.clean();
		});

		it('Manifest was not used', () => {
			assert.equal(testPlugin.used, false, 'manifest not used because of lockfile mismatch');
		});
	});
});