-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcacheStore.test.ts
More file actions
157 lines (142 loc) · 4.77 KB
/
Copy pathcacheStore.test.ts
File metadata and controls
157 lines (142 loc) · 4.77 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {
existsSync,
mkdirSync,
readFileSync,
readdirSync,
writeFileSync,
} from 'node:fs';
import path from 'node:path';
import { expect, test } from 'rstack/test';
import {
fmtCacheFileName,
fmtCacheVersion,
loadFmtCacheStore,
type FmtCacheFile,
} from '../../src/fmt/cacheStore.ts';
import { withTempProject } from './helpers.ts';
const namespace = 'test-namespace';
const contentA = 'content-a';
const contentB = 'content-b';
const contentC = 'content-c';
const optionsA = 'options-a';
const optionsB = 'options-b';
const optionsC = 'options-c';
const firstEntry = [contentA, optionsA, 'clean'] as const;
const secondEntry = [contentB, optionsB, 'dirty'] as const;
const unsupportedEntry = ['', optionsC, 'unsupported'] as const;
const hashedUnsupportedEntry = [contentC, optionsC, 'unsupported'] as const;
const readCache = (filePath: string): FmtCacheFile =>
JSON.parse(readFileSync(filePath, 'utf8')) as FmtCacheFile;
test('writes flat entries that can be loaded by another store', async () => {
await withTempProject(async (rootPath) => {
const cachePath = path.join(rootPath, 'cache', fmtCacheFileName);
const store = await loadFmtCacheStore(cachePath, namespace);
expect(await store.save()).toBe(false);
expect(existsSync(cachePath)).toBe(false);
store.set('src/a.ts', firstEntry);
store.set('src/unknown.fixture', unsupportedEntry);
store.set('script', hashedUnsupportedEntry);
expect(await store.save()).toBe(true);
expect(await store.save()).toBe(false);
expect(readCache(cachePath)).toEqual({
version: fmtCacheVersion,
namespace,
options: [optionsA, optionsC],
files: [
'src/a.ts',
contentA,
0,
0,
'src/unknown.fixture',
'',
1,
2,
'script',
contentC,
1,
2,
],
});
const loaded = await loadFmtCacheStore(cachePath, namespace);
expect(loaded.get('src/a.ts')).toEqual(firstEntry);
expect(loaded.get('src/unknown.fixture')).toEqual(unsupportedEntry);
expect(loaded.get('script')).toEqual(hashedUnsupportedEntry);
});
});
test('preserves unvisited entries and skips unchanged updates', async () => {
await withTempProject(async (rootPath) => {
const cachePath = path.join(rootPath, fmtCacheFileName);
writeFileSync(
cachePath,
`${JSON.stringify({
version: fmtCacheVersion,
namespace,
options: [optionsA, optionsB],
files: ['src/a.ts', contentA, 0, 0, 'src/b.ts', contentB, 1, 1],
})}\n`,
);
const store = await loadFmtCacheStore(cachePath, namespace);
store.set('src/a.ts', secondEntry);
store.set('src/a.ts', firstEntry);
expect(await store.save()).toBe(false);
store.set('src/a.ts', secondEntry);
expect(await store.save()).toBe(true);
expect(readCache(cachePath)).toEqual({
version: fmtCacheVersion,
namespace,
options: [optionsB],
files: ['src/a.ts', contentB, 0, 1, 'src/b.ts', contentB, 0, 1],
});
});
});
test('discards invalid schemas and other namespaces', async () => {
await withTempProject(async (rootPath) => {
const cachePath = path.join(rootPath, fmtCacheFileName);
const validCache = {
version: fmtCacheVersion,
namespace,
options: [optionsA],
files: ['src/a.ts', contentA, 0, 0],
};
const invalidContents = [
'{invalid',
JSON.stringify({ ...validCache, version: fmtCacheVersion - 1 }),
JSON.stringify({ version: fmtCacheVersion, namespace, files: [] }),
JSON.stringify({ ...validCache, files: { 'src/a.ts': firstEntry } }),
JSON.stringify({ ...validCache, files: ['src/a.ts', contentA, 0] }),
];
for (const content of invalidContents) {
writeFileSync(cachePath, content);
const store = await loadFmtCacheStore(cachePath, namespace);
expect(store.get('src/a.ts')).toBeUndefined();
}
writeFileSync(
cachePath,
JSON.stringify({
...validCache,
namespace: 'old-namespace',
}),
);
const store = await loadFmtCacheStore(cachePath, namespace);
expect(store.get('src/a.ts')).toBeUndefined();
expect(await store.save()).toBe(true);
expect(readCache(cachePath)).toEqual({
version: fmtCacheVersion,
namespace,
options: [],
files: [],
});
});
});
test('does not throw or leave temporary files when persistence fails', async () => {
await withTempProject(async (rootPath) => {
const cachePath = path.join(rootPath, fmtCacheFileName);
mkdirSync(cachePath);
const store = await loadFmtCacheStore(cachePath, namespace);
store.set('src/a.ts', firstEntry);
await expect(store.save()).resolves.toBe(false);
expect(
readdirSync(rootPath).filter((name) => name.endsWith('.tmp')),
).toEqual([]);
});
});