Repository navigation
Expand file tree
/
Copy pathextensionModule.test.js
More file actions
152 lines (137 loc) · 5.73 KB
/
Copy pathextensionModule.test.js
File metadata and controls
152 lines (137 loc) · 5.73 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
import { EventEmitter } from 'node:events';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const regenerateAll = vi.hoisted(() => vi.fn(async () => {}));
vi.mock('./utils/regenerateAll.js', async (importOriginal) => ({
.../** @type {object} */ (await importOriginal()),
regenerateAll,
}));
const { handleApplication } = await import('./extensionModule.js');
/**
* @param {Record<string, unknown>} options
*/
function fakeScope(options) {
const scope = Object.assign(new EventEmitter(), {
logger: { trace: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
options: { get: (/** @type {string[]} */ [key]) => options[key] },
databaseEvents: new EventEmitter(),
directory: '/apps/demo',
resources: { allTypes: new Map() },
handleEntry: vi.fn(),
});
return scope;
}
describe('handleApplication', () => {
beforeEach(() => {
vi.useFakeTimers();
process.env.DEV_MODE = 'true';
});
afterEach(() => {
vi.useRealTimers();
delete process.env.DEV_MODE;
vi.clearAllMocks();
});
it('does nothing outside dev mode', async () => {
delete process.env.DEV_MODE;
const scope = fakeScope({ schemaTypes: 'types.ts' });
await handleApplication(/** @type {any} */ (scope));
await vi.advanceTimersByTimeAsync(10_000);
expect(regenerateAll).not.toHaveBeenCalled();
});
it('generates after the initial delay, resolving outputs against the app directory', async () => {
const scope = fakeScope({
schemaTypes: 'types.ts',
swift: 'ios/HarperModels',
swiftModule: 'Demo',
});
await handleApplication(/** @type {any} */ (scope));
await vi.advanceTimersByTimeAsync(4_999);
expect(regenerateAll).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(200);
expect(regenerateAll).toHaveBeenCalledTimes(1);
const [, schemaTypes, , options] = regenerateAll.mock.calls[0];
expect(schemaTypes).toBe('types.ts');
expect(options).toMatchObject({
baseDirectory: '/apps/demo',
client: { swift: 'ios/HarperModels', swiftModule: 'Demo', types: scope.resources.allTypes },
});
});
it('reads the type registry on every run', async () => {
const scope = fakeScope({ schemaTypes: 'types.ts', schemaIR: 'ir.json' });
await handleApplication(/** @type {any} */ (scope));
await vi.advanceTimersByTimeAsync(5_200);
const replaced = new Map([['Address', { attributes: [] }]]);
scope.resources.allTypes = replaced;
scope.databaseEvents.emit('updateTable');
await vi.advanceTimersByTimeAsync(200);
expect(regenerateAll.mock.calls.at(-1)?.[3]).toMatchObject({ client: { types: replaced } });
});
it('aborts a run still in flight when the scope closes', async () => {
const scope = fakeScope({ schemaTypes: 'types.ts', schemaIR: 'ir.json' });
await handleApplication(/** @type {any} */ (scope));
await vi.advanceTimersByTimeAsync(5_200);
const { signal } = /** @type {any} */ (regenerateAll.mock.calls[0][3]).client;
expect(signal.aborted).toBe(false);
scope.emit('close');
expect(signal.aborted).toBe(true);
});
it('coalesces a burst of schema events into one regeneration', async () => {
const scope = fakeScope({ schemaTypes: 'types.ts' });
await handleApplication(/** @type {any} */ (scope));
await vi.advanceTimersByTimeAsync(5_200);
regenerateAll.mockClear();
for (let i = 0; i < 5; i++) scope.databaseEvents.emit('updateTable');
scope.databaseEvents.emit('dropTable');
await vi.advanceTimersByTimeAsync(200);
expect(regenerateAll).toHaveBeenCalledTimes(1);
});
it('installs nothing when the scope closes before the initial delay', async () => {
const scope = fakeScope({ schemaTypes: 'types.ts' });
await handleApplication(/** @type {any} */ (scope));
scope.emit('close');
await vi.advanceTimersByTimeAsync(10_000);
expect(regenerateAll).not.toHaveBeenCalled();
expect(scope.databaseEvents.listenerCount('updateTable')).toBe(0);
});
it('stops listening when the scope closes', async () => {
const scope = fakeScope({ schemaTypes: 'types.ts' });
await handleApplication(/** @type {any} */ (scope));
await vi.advanceTimersByTimeAsync(5_200);
scope.emit('close');
regenerateAll.mockClear();
scope.databaseEvents.emit('updateTable');
await vi.advanceTimersByTimeAsync(1_000);
expect(regenerateAll).not.toHaveBeenCalled();
expect(scope.databaseEvents.listenerCount('updateTable')).toBe(0);
});
it('logs a failed regeneration instead of throwing into Harper', async () => {
regenerateAll.mockRejectedValueOnce(new Error('disk full'));
const scope = fakeScope({ schemaTypes: 'types.ts' });
await handleApplication(/** @type {any} */ (scope));
await vi.advanceTimersByTimeAsync(5_200);
expect(scope.logger.error).toHaveBeenCalledWith(expect.stringContaining('disk full'));
});
it('watches sync.yaml only when a client output is configured', async () => {
const withClient = fakeScope({ syncProfiles: 'sync.yaml', kotlin: 'android' });
await handleApplication(/** @type {any} */ (withClient));
expect(withClient.handleEntry).toHaveBeenCalledWith(
{ files: 'sync.yaml' },
expect.any(Function),
);
const profilesOnly = fakeScope({ syncProfiles: 'sync.yaml' });
await handleApplication(/** @type {any} */ (profilesOnly));
expect(profilesOnly.handleEntry).not.toHaveBeenCalled();
});
it('regenerates on sync.yaml changes once started', async () => {
const scope = fakeScope({ syncProfiles: 'sync.yaml', schemaIR: 'ir.json' });
await handleApplication(/** @type {any} */ (scope));
const onEntry = scope.handleEntry.mock.calls[0][1];
onEntry({ eventType: 'add' });
await vi.advanceTimersByTimeAsync(1_000);
expect(regenerateAll).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(4_200);
regenerateAll.mockClear();
onEntry({ eventType: 'change' });
await vi.advanceTimersByTimeAsync(200);
expect(regenerateAll).toHaveBeenCalledTimes(1);
});
});