This repository was archived by the owner on Nov 4, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathvite-objectstack-client-dist.test.ts
More file actions
457 lines (417 loc) · 21.6 KB
/
Copy pathvite-objectstack-client-dist.test.ts
File metadata and controls
457 lines (417 loc) · 21.6 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import {
CLIENT_PACKAGE_NAME,
resolveClientDistInjection,
widenVendorChunkTestForClient,
} from '../vite-objectstack-client-dist';
/**
* objectui#6094 — `OBJECTSTACK_CLIENT_DIST` validates the override it aliases.
*
* The hook lets a developer point the console at a locally built
* `@objectstack/client`. Before this card it was a bare string alias: no
* existence check, no manifest read, no dependency check. Measured on
* `a100f77d3` with the override aimed at a copy of the installed client placed
* outside the workspace, `vite build` printed `✓ 8615 modules transformed`,
* wrote every chunk, and left the client's three own bare specifiers
* (`@objectstack/core/logger`, `@objectstack/spec/api`, `@objectstack/spec/data`)
* in `assets/framework-*.js` as calls to rolldown's `require` shim — a bundle no
* browser can load, produced by a build whose output never named the variable.
*
* Three facts are pinned here, and they pull against each other on purpose:
*
* 1. **Set and broken → refused, by NAME.** The card's complaint is not that
* the build survived; it is that nothing named `OBJECTSTACK_CLIENT_DIST`.
* So the cases below assert the variable appears in the message, not
* merely that something threw.
* 2. **Set and valid → still injected.** A check that refused every override
* would pass (1) while destroying the hook's only purpose. Both valid
* spellings are exercised against the REAL installed client.
* 3. **Unset → nothing moves.** The alias table and `server.fs.allow` stay at
* their baseline values, read off the real console config, so a check that
* stopped being conditional turns red here rather than shipping a
* different production bundle.
*
* The shape axis cuts across all three: a directory, its `dist/`, and an entry
* file inside it are all legal, and a dependency check is parameterised over a
* DIRECTORY. A check that quietly did nothing for the entry-file spelling would
* be the same defect wearing a green build, so every refusal case is asserted
* for both spellings.
*
* Reverse verification — direction predicted BEFORE each run, and one
* prediction was wrong in a way worth keeping:
*
* - **Start the walk at `resolved` even for a file** (drop the
* `shape === 'directory'` branch in `findClientPackageDir`). Predicted RED
* on the entry-file cases. Measured **GREEN, 19/19** — and that is a fact
* about the code, not a vacuous suite. The walk probes
* `<dir>/package.json` and climbs; handed `…/dist/index.mjs` its first
* probe simply misses (`…/index.mjs/package.json`) and the next iteration
* lands on `…/dist` anyway. The branch is explicitness about where the walk
* starts, not the mechanism that makes the file spelling work — the walk
* itself is indifferent. Recorded rather than deleted: a green ablation
* left unexplained reads as a test that pins nothing.
* - **Skip validation entirely for the entry-file spelling** — the naive port
* of a directory-parameterised check, and the exact defect class the card
* names ("a check that silently does nothing when pointed at a
* `dist/index.js`"). Predicted RED on the entry-file cases; measured 3 red,
* including *refuses an out-of-tree copy given as an entry file*. That is
* the pin the first leg was reaching for.
* - **Drop the `realpathSync`** in `findClientPackageDir`. Predicted RED on
* the valid installed-client cases, because pnpm reaches the client through
* a symlink whose ancestors never include the store directory holding its
* dependencies. Measured 5 red, every one of them "a VALID override is
* rejected" — the direction that matters, since a suite blind to it would
* have shipped a hook that refuses everything and still passed the
* reproduction.
*/
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
/**
* The installed client, as the console resolves it — under pnpm a SYMLINK into
* the store, which is the whole point of the realpath cases below.
*/
const installedClientDir = path.join(repoRoot, 'apps/console/node_modules/@objectstack/client');
/** Fixture roots created here, removed in `afterAll`. */
const fixtureRoots: string[] = [];
function makeFixtureRoot(): string {
// Each fixture gets its OWN `mkdtempSync` root rather than sharing
// `os.tmpdir()`, which every other suite and every parallel agent also writes
// to — and, more to the point here, the walk under test climbs its ancestors.
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'client-dist-6094-'));
fixtureRoots.push(root);
return root;
}
/**
* A copy of the installed client placed outside the workspace — manifest and
* `dist/` intact, no reachable `node_modules` for its two dependencies.
*
* This is the card's reproduction, reduced to a fixture. The package's OWN
* `node_modules` stays behind: a store copy has none (its dependencies are its
* siblings in the store), but under the objectstack monorepo the installed
* client is a workspace link to a package that carries one, and copying that
* would carry the dependencies along (objectstack#25733).
*/
function outOfTreeClientCopy(): string {
const dest = path.join(makeFixtureRoot(), 'client');
const source = fs.realpathSync(installedClientDir);
const ownNodeModules = path.join(source, 'node_modules');
fs.cpSync(source, dest, { recursive: true, dereference: true, filter: (src) => src !== ownNodeModules });
return dest;
}
/**
* pnpm's store layout, on disk: the client in
* `.pnpm/@objectstack+client@1.0.0/node_modules/@objectstack/client` with its
* dependency as a sibling there, and a console whose `node_modules` holds only
* a SYMLINK to it. That is how a standalone checkout installs the client; under
* the objectstack monorepo it is a workspace link instead, so the realpath
* measurement below builds this layout rather than reading the checkout's
* (objectstack#25733).
*/
function storeLayoutClient(): { linked: string; real: string } {
const root = makeFixtureRoot();
const storeModules = path.join(root, 'node_modules/.pnpm/@objectstack+client@1.0.0/node_modules');
const real = path.join(storeModules, '@objectstack/client');
fs.mkdirSync(real, { recursive: true });
fs.writeFileSync(
path.join(real, 'package.json'),
JSON.stringify({ name: CLIENT_PACKAGE_NAME, dependencies: { '@objectstack/core': '1.0.0' } })
);
fs.mkdirSync(path.join(storeModules, '@objectstack/core'), { recursive: true });
fs.writeFileSync(path.join(storeModules, '@objectstack/core/package.json'), '{"name":"@objectstack/core"}');
const linked = path.join(root, 'console/node_modules/@objectstack/client');
fs.mkdirSync(path.dirname(linked), { recursive: true });
fs.symlinkSync(real, linked, 'dir');
return { linked, real: fs.realpathSync(real) };
}
/**
* The walk `packageResolvesFrom` performs, re-stated here as a MEASUREMENT
* rather than an import: these cases exist to show what the production walk
* would answer if it ran from the un-realpath'd path, which is precisely what
* the module refuses to do.
*/
function walkResolves(startDir: string, name: string): boolean {
let dir = startDir;
for (;;) {
if (fs.existsSync(path.join(dir, 'node_modules', name))) return true;
const parent = path.dirname(dir);
if (parent === dir) return false;
dir = parent;
}
}
/**
* A fresh evaluation of the console's Vite config, keyed by `query`.
*
* The specifier is assembled at runtime on purpose — a literal one would pull
* `apps/console/vite.config.ts` into this program, where `tsconfig.scripts.json`
* (no `allowImportingTsExtensions`) turns its own `.ts` imports into TS5097.
* Same reasoning, same spelling as the spec-dist suite next door.
*/
async function loadConsoleConfig(query = ''): Promise<any> {
const specifier = `../../apps/console/vite.config.ts${query}`;
return (await import(/* @vite-ignore */ specifier)).default;
}
beforeAll(() => {
// Every case below reads the installed client. If the workspace is not
// installed, say so here rather than letting each case invent its own
// explanation for a missing directory.
expect(fs.existsSync(path.join(installedClientDir, 'package.json'))).toBe(true);
});
afterAll(() => {
for (const root of fixtureRoots) fs.rmSync(root, { recursive: true, force: true });
});
describe('objectui#6094: unset leaves the hook inert', () => {
it.each([undefined, '', ' '])('returns null for %j', (raw) => {
expect(resolveClientDistInjection(raw)).toBeNull();
});
it('leaves the console config at its baseline alias and fs.allow', async () => {
const config = await loadConsoleConfig();
expect(config.resolve.alias[CLIENT_PACKAGE_NAME]).toBeUndefined();
// `server.fs` is absent entirely when neither override is set, so Vite
// keeps its own default allow-list.
expect(config.server.fs).toBeUndefined();
});
});
describe('objectui#6094: a VALID override is still injected — all three spellings', () => {
const realClientDir = () => fs.realpathSync(installedClientDir);
it('accepts the package directory', () => {
const injection = resolveClientDistInjection(installedClientDir);
expect(injection).not.toBeNull();
expect(injection!.aliasTarget).toBe(installedClientDir);
expect(injection!.packageDir).toBe(realClientDir());
});
it('accepts the `dist/` directory inside the package', () => {
const dist = path.join(installedClientDir, 'dist');
const injection = resolveClientDistInjection(dist);
expect(injection!.aliasTarget).toBe(dist);
// The walk climbed out of `dist/` to the package that owns it.
expect(injection!.packageDir).toBe(realClientDir());
});
it('accepts a built entry FILE inside the package', () => {
const entry = path.join(installedClientDir, 'dist/index.mjs');
expect(fs.statSync(entry).isFile()).toBe(true);
const injection = resolveClientDistInjection(entry);
expect(injection!.aliasTarget).toBe(entry);
expect(injection!.packageDir).toBe(realClientDir());
// The alias target is the file, unchanged from the pre-check config; only
// the dependency walk uses the package directory.
expect(injection!.fsAllow).toEqual([
path.join(installedClientDir, 'dist'),
installedClientDir,
]);
});
it('resolves the dependency walk through the pnpm symlink, not around it', () => {
// The measurement that makes `realpathSync` load-bearing rather than
// tidy, on the store layout: from the symlink path, the client's own
// dependency is NOT found — the walk climbs `console/node_modules` →
// `console` → the fixture root, none of which carry
// `node_modules/@objectstack/core`. From the store path it is found,
// beside the store copy of the client.
const { linked, real } = storeLayoutClient();
expect(real).not.toBe(linked);
expect(walkResolves(linked, '@objectstack/core')).toBe(false);
expect(walkResolves(real, '@objectstack/core')).toBe(true);
// …so the injection, which walks from `packageDir`, must be reading the
// store path. Without this the check would reject the installed client —
// the one input it must accept.
expect(resolveClientDistInjection(linked)!.packageDir).toBe(real);
// And this checkout's own installed client, store copy or workspace link,
// is judged at its real path too.
expect(resolveClientDistInjection(installedClientDir)!.packageDir).toBe(realClientDir());
});
it('does not demand `peerDependencies` — they come from the consuming app', () => {
const dir = path.join(makeFixtureRoot(), 'client');
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(
path.join(dir, 'package.json'),
JSON.stringify({ name: CLIENT_PACKAGE_NAME, peerDependencies: { react: '^19' } })
);
expect(resolveClientDistInjection(dir)).not.toBeNull();
});
it('tolerates a manifest with no `dependencies` at all', () => {
const dir = path.join(makeFixtureRoot(), 'client');
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({ name: CLIENT_PACKAGE_NAME }));
expect(resolveClientDistInjection(dir)).not.toBeNull();
});
it('injects into the real console config: alias plus a widened fs.allow', async () => {
const baseline = await loadConsoleConfig();
process.env.OBJECTSTACK_CLIENT_DIST = installedClientDir;
let injected: any;
try {
// A distinct module id (the query suffix) so the baseline evaluation
// above stays intact and the two can be compared. `vi.resetModules()` is
// not an option: the `unit` project runs `isolate: false`.
injected = await loadConsoleConfig('?objectstack-client-dist=6094');
} finally {
delete process.env.OBJECTSTACK_CLIENT_DIST;
}
expect(injected.resolve.alias[CLIENT_PACKAGE_NAME]).toBe(installedClientDir);
expect(injected.server.fs.allow).toEqual([
repoRoot,
path.dirname(installedClientDir),
path.resolve(path.dirname(installedClientDir), '..'),
]);
// …and the baseline evaluation is untouched by the second one, which is
// what makes the inert case above meaningful.
expect(baseline.resolve.alias[CLIENT_PACKAGE_NAME]).toBeUndefined();
});
});
describe('objectui#6094: a BROKEN override is refused, naming the variable', () => {
it('refuses a path that does not exist', () => {
const missing = path.join(makeFixtureRoot(), 'no-such-client');
expect(() => resolveClientDistInjection(missing)).toThrow(/OBJECTSTACK_CLIENT_DIST/);
expect(() => resolveClientDistInjection(missing)).toThrow(/does not exist/);
});
// The card's reproduction, both spellings. The entry-file row is the one that
// matters most: a check parameterised over a package directory, handed a
// file, is exactly where "silently does nothing" hides.
const brokenShapes = [
['directory', (root: string) => root],
['`dist/` directory', (root: string) => path.join(root, 'dist')],
['entry file', (root: string) => path.join(root, 'dist/index.mjs')],
] as const;
it.each(brokenShapes)(
'refuses an out-of-tree copy given as a %s, naming every unresolved dependency',
(_label, pick) => {
const copy = outOfTreeClientCopy();
const value = pick(copy);
expect(fs.existsSync(value)).toBe(true);
let thrown: Error | undefined;
try {
resolveClientDistInjection(value);
} catch (error) {
thrown = error as Error;
}
expect(thrown).toBeDefined();
const message = thrown!.message;
// 1. It names the variable — the card's actual complaint.
expect(message).toContain('OBJECTSTACK_CLIENT_DIST');
// 2. It names the dependencies that do not resolve, both of them: these
// are the packages owning the three bare specifiers that survived into
// the bundle (`@objectstack/core/logger`, `@objectstack/spec/api`,
// `@objectstack/spec/data`).
expect(message).toContain('@objectstack/core');
expect(message).toContain('@objectstack/spec');
// 3. It names the package directory the walk judged, so a reader can see
// WHICH tree was searched — the fact that decides whether the fix is
// "install deps there" or "point somewhere else".
expect(message).toContain(fs.realpathSync(copy));
// 4. It is the dependency verdict, not the shape verdict: a check that
// failed the entry-file row with "not inside a package" would satisfy
// (1) while telling the reader nothing true.
expect(message).toMatch(/do(es)? not resolve from/);
}
);
it('refuses a bare `dist/` copied out with no manifest anywhere above it', () => {
// The other half of the card's reproduction: copy only `dist/`, and there
// is no `package.json` in any ancestor — so there is no manifest to read
// dependencies from, and the refusal has to say THAT rather than invent a
// dependency verdict.
const root = makeFixtureRoot();
fs.cpSync(path.join(fs.realpathSync(installedClientDir), 'dist'), path.join(root, 'dist'), {
recursive: true,
dereference: true,
});
for (const value of [path.join(root, 'dist'), path.join(root, 'dist/index.mjs')]) {
expect(() => resolveClientDistInjection(value)).toThrow(/OBJECTSTACK_CLIENT_DIST/);
expect(() => resolveClientDistInjection(value)).toThrow(/is not inside a/);
expect(() => resolveClientDistInjection(value)).toThrow(/@objectstack\/client/);
}
});
it('refuses a path whose nearest manifest is some OTHER package', () => {
// Without the name match the walk would stop at the first `package.json`
// above the value and validate THAT package's dependencies — a check that
// always passes, on the wrong subject. `apps/console` is a real package
// with real, resolvable dependencies, so it would pass exactly that way.
const consoleDir = path.join(repoRoot, 'apps/console');
expect(() => resolveClientDistInjection(consoleDir)).toThrow(/is not inside a/);
expect(() => resolveClientDistInjection(consoleDir)).toThrow(/OBJECTSTACK_CLIENT_DIST/);
});
it('refuses an unreadable manifest instead of skipping past it', () => {
const dir = path.join(makeFixtureRoot(), 'client');
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, 'package.json'), '{ not json');
expect(() => resolveClientDistInjection(dir)).toThrow(/is not readable JSON/);
expect(() => resolveClientDistInjection(dir)).toThrow(/OBJECTSTACK_CLIENT_DIST/);
});
it('fails the console config LOAD, before any module is bundled', async () => {
// Where the refusal lands is the whole value of the card: at config
// evaluation, with the variable named — not a build later, in a resolve
// error naming a specifier the reader never typed.
const copy = outOfTreeClientCopy();
process.env.OBJECTSTACK_CLIENT_DIST = copy;
try {
await expect(loadConsoleConfig('?objectstack-client-dist=6094-broken')).rejects.toThrow(
/OBJECTSTACK_CLIENT_DIST/
);
} finally {
delete process.env.OBJECTSTACK_CLIENT_DIST;
}
});
});
/**
* An injected client stays in `vendor-objectstack`.
*
* objectstack's `build-console.sh` always injects its own client, from a
* workspace path (`<framework>/packages/client`) that the baseline group test —
* written against `node_modules/@objectstack/` and pnpm's `@objectstack+` store
* paths — does not match. Unmatched, the client landed in `framework` while
* `data-adapter` required it at top level, and the console died at boot with
* `TypeError: u is not a function` (measured on `9f0c84a44`). The fixture below
* reproduces that layout: a client package outside any `node_modules`, its
* dependencies reachable from a workspace-root `node_modules`.
*
* Reverse verification: with `vendorObjectstackTest` back on the bare
* `specDistInjection ? … : VENDOR_OBJECTSTACK_TEST` expression, the config case
* below goes red (the injected module id is not claimed) while the helper cases
* stay green — the config case is the one that pins the wiring.
*/
describe('an injected client joins the vendor-objectstack group', () => {
function workspaceClient(): string {
const root = makeFixtureRoot();
const clientDir = path.join(root, 'packages', 'client');
fs.cpSync(fs.realpathSync(installedClientDir), clientDir, { recursive: true, dereference: true });
for (const dep of ['core', 'spec']) {
fs.mkdirSync(path.join(root, 'node_modules', '@objectstack', dep), { recursive: true });
}
return clientDir;
}
const BASELINE = /([\\/]node_modules[\\/]@objectstack[\\/](?!lint[\\/])|[\\/]@objectstack\+(?!lint@))/;
it('returns the baseline test itself when nothing is injected', () => {
expect(widenVendorChunkTestForClient(BASELINE, null)).toBe(BASELINE);
});
it('claims a module under the injected package, and still everything the baseline claimed', () => {
const clientDir = workspaceClient();
const injection = resolveClientDistInjection(clientDir);
expect(injection).not.toBeNull();
const moduleId = path.join(fs.realpathSync(clientDir), 'dist', 'index.js').split(path.sep).join('/');
expect(BASELINE.test(moduleId)).toBe(false);
const widened = widenVendorChunkTestForClient(BASELINE, injection);
expect(widened.test(moduleId)).toBe(true);
expect(widened.test('/r/node_modules/@objectstack/spec/dist/index.mjs')).toBe(true);
expect(widened.test('/r/node_modules/.pnpm/@objectstack+core@1.0.0/x.js')).toBe(true);
// A sibling directory that merely shares the prefix is not claimed.
expect(widened.test(`${fs.realpathSync(clientDir).split(path.sep).join('/')}-other/dist/index.js`)).toBe(false);
});
it('widens the vendor-objectstack group of the real console config', async () => {
const clientDir = workspaceClient();
const moduleId = path.join(fs.realpathSync(clientDir), 'dist', 'index.js').split(path.sep).join('/');
const vendorTest = (config: any): RegExp =>
config.build.rollupOptions.output.advancedChunks.groups.find(
(g: { name: string }) => g.name === 'vendor-objectstack'
).test;
const baseline = await loadConsoleConfig();
process.env.OBJECTSTACK_CLIENT_DIST = clientDir;
let injected: any;
try {
injected = await loadConsoleConfig('?objectstack-client-dist=vendor-chunk');
} finally {
delete process.env.OBJECTSTACK_CLIENT_DIST;
}
expect(vendorTest(baseline).test(moduleId)).toBe(false);
expect(vendorTest(injected).test(moduleId)).toBe(true);
});
});