Skip to content

Commit 4e4d98a

Browse files
committed
test(cli): pin the local-customization read and notice (WIP)
Claude-Session: https://claude.ai/code/session_01Utp6CbrroXG7sXSzmQs4uj Co-authored-by: Claude <noreply@anthropic.com>
1 parent fa3cfe0 commit 4e4d98a

2 files changed

Lines changed: 205 additions & 0 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// The integration half of `os build`'s local-customization notice: the read
4+
// against a real SQLite file, the shape `os dev` leaves in
5+
// `.objectstack/data/objectstack.db` (a `sys_metadata` row per stored item,
6+
// keyed by `package_id`). The pure half is `local-customizations.test.ts`.
7+
8+
import { describe, it, expect, afterEach } from 'vitest';
9+
import fs from 'node:fs';
10+
import os from 'node:os';
11+
import path from 'node:path';
12+
import Database from 'better-sqlite3';
13+
import { readCustomizationsFromSqlite, readLocalCustomizations } from './local-customizations.js';
14+
15+
const dirs: string[] = [];
16+
function tempProject(): string {
17+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'os-local-customizations-db-'));
18+
dirs.push(dir);
19+
return dir;
20+
}
21+
afterEach(() => {
22+
for (const d of dirs.splice(0)) fs.rmSync(d, { recursive: true, force: true });
23+
});
24+
25+
function devDatabase(root: string, rows: Array<[type: string, name: string, packageId: string | null]>): string {
26+
const file = path.join(root, '.objectstack', 'data', 'objectstack.db');
27+
fs.mkdirSync(path.dirname(file), { recursive: true });
28+
const db = new Database(file);
29+
db.exec('CREATE TABLE sys_metadata (id TEXT PRIMARY KEY, type TEXT, name TEXT, package_id TEXT, state TEXT)');
30+
const insert = db.prepare('INSERT INTO sys_metadata (id, type, name, package_id, state) VALUES (?, ?, ?, ?, ?)');
31+
rows.forEach(([type, name, packageId], i) => insert.run(`m${i}`, type, name, packageId, 'active'));
32+
db.close();
33+
return file;
34+
}
35+
36+
describe('reading the stored `.custom` items of the dev database', () => {
37+
it('counts the items of each `.custom` package, and nothing else', async () => {
38+
const root = tempProject();
39+
const file = devDatabase(root, [
40+
['view', 'asset_list', 'com.example.asset-repair.custom'],
41+
['dashboard', 'ops', 'com.example.asset-repair.custom'],
42+
['view', 'lead_list', 'com.vendor.crm.custom'],
43+
['view', 'authored', 'com.example.studio-app'],
44+
['view', 'packageless', null],
45+
// A package whose id merely contains the suffix is not a pair.
46+
['view', 'decoy', 'com.custom.tools'],
47+
// LIKE's `_` must not stand in for the suffix's dot.
48+
['view', 'decoy2', 'com.example_custom'],
49+
]);
50+
51+
const reading = await readLocalCustomizations({ projectRoot: root, env: {} });
52+
expect(reading).toEqual({
53+
status: 'found',
54+
filename: file,
55+
packages: [
56+
{ id: 'com.example.asset-repair.custom', items: 2 },
57+
{ id: 'com.vendor.crm.custom', items: 1 },
58+
],
59+
});
60+
});
61+
62+
it('answers none for a database with no `.custom` item, or not yet booted into tables', async () => {
63+
const root = tempProject();
64+
const file = devDatabase(root, [['view', 'authored', 'com.example.studio-app']]);
65+
expect(await readCustomizationsFromSqlite(file)).toEqual({ status: 'none', filename: file });
66+
67+
const empty = path.join(tempProject(), 'empty.db');
68+
new Database(empty).close();
69+
expect(await readCustomizationsFromSqlite(empty)).toEqual({ status: 'none', filename: empty });
70+
});
71+
72+
it('opens the file read-only: it is byte-identical after the read', async () => {
73+
const root = tempProject();
74+
const file = devDatabase(root, [['view', 'asset_list', 'com.example.app.custom']]);
75+
const before = fs.readFileSync(file);
76+
await readCustomizationsFromSqlite(file);
77+
expect(fs.readFileSync(file).equals(before)).toBe(true);
78+
});
79+
80+
it('answers unknown, with the reason, for a file that is not a database', async () => {
81+
const file = path.join(tempProject(), 'objectstack.db');
82+
fs.writeFileSync(file, 'not a sqlite database, just text that is long enough to have a header'.repeat(10));
83+
const reading = await readCustomizationsFromSqlite(file);
84+
expect(reading.status).toBe('unknown');
85+
expect(reading).toMatchObject({ filename: file });
86+
});
87+
88+
it('follows OS_DATABASE_URL to the file `os dev` would open', async () => {
89+
const root = tempProject();
90+
const elsewhere = devDatabase(tempProject(), [['view', 'v', 'com.example.app.custom']]);
91+
const reading = await readLocalCustomizations({ projectRoot: root, env: { OS_DATABASE_URL: `file:${elsewhere}` } });
92+
expect(reading).toMatchObject({ status: 'found', filename: elsewhere });
93+
});
94+
});
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// The pure half of `os build`'s local-customization notice: which database it
4+
// reads, which file a URL names, what it says for each reading, and that only
5+
// `os build` says it. Reading a real SQLite file is the integration half
6+
// (`local-customizations.sqlite.test.ts`).
7+
8+
import { describe, it, expect, afterEach } from 'vitest';
9+
import fs from 'node:fs';
10+
import os from 'node:os';
11+
import path from 'node:path';
12+
import { sqliteFilenameFromUrl } from '@objectstack/runtime';
13+
import {
14+
devDatabaseEnv,
15+
localSqliteFile,
16+
localCustomizationNotice,
17+
readCustomizationsFromSqlite,
18+
readLocalCustomizations,
19+
} from './local-customizations.js';
20+
import Build from '../commands/build.js';
21+
import Compile from '../commands/compile.js';
22+
23+
const dirs: string[] = [];
24+
function tempProject(): string {
25+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'os-local-customizations-'));
26+
dirs.push(dir);
27+
return dir;
28+
}
29+
afterEach(() => {
30+
for (const d of dirs.splice(0)) fs.rmSync(d, { recursive: true, force: true });
31+
});
32+
33+
describe('the database the notice reads is the one `os dev` opens', () => {
34+
it('layers the development .env files under the process environment, without loading them', () => {
35+
const root = tempProject();
36+
fs.writeFileSync(path.join(root, '.env'), 'OS_DATABASE_URL=file:./from-env-file.db\nOS_ONLY_IN_FILE=1\n');
37+
fs.writeFileSync(path.join(root, '.env.development'), 'OS_DATABASE_URL=file:./from-development.db\n');
38+
39+
const fromFiles = devDatabaseEnv(root, {});
40+
expect(fromFiles.OS_DATABASE_URL).toBe('file:./from-development.db');
41+
expect(fromFiles.OS_ONLY_IN_FILE).toBe('1');
42+
43+
// dotenv-flow never overrides a variable the process already has.
44+
expect(devDatabaseEnv(root, { OS_DATABASE_URL: 'file:/from-shell.db' }).OS_DATABASE_URL).toBe('file:/from-shell.db');
45+
expect(process.env.OS_ONLY_IN_FILE).toBeUndefined();
46+
});
47+
48+
it('names a file only for a local SQLite URL', () => {
49+
expect(localSqliteFile('file:/tmp/x/objectstack.db', sqliteFilenameFromUrl)).toBe('/tmp/x/objectstack.db');
50+
expect(localSqliteFile('wasm-sqlite:///tmp/x/objectstack.db', sqliteFilenameFromUrl)).toBe('/tmp/x/objectstack.db');
51+
expect(localSqliteFile('file::memory:', sqliteFilenameFromUrl)).toBeNull();
52+
expect(localSqliteFile(':memory:', sqliteFilenameFromUrl)).toBeNull();
53+
expect(localSqliteFile('postgres://u:p@db:5432/app', sqliteFilenameFromUrl)).toBeNull();
54+
expect(localSqliteFile('libsql://app.turso.io', sqliteFilenameFromUrl)).toBeNull();
55+
});
56+
57+
it('reads nothing when the resolved database is remote or not created yet', async () => {
58+
const root = tempProject();
59+
expect(await readLocalCustomizations({ projectRoot: root, env: { OS_DATABASE_URL: 'postgres://u:p@db:5432/app' } }))
60+
.toEqual({ status: 'not_applicable' });
61+
// The unified default `<root>/.objectstack/data/objectstack.db`, never booted.
62+
expect(await readLocalCustomizations({ projectRoot: root, env: {} })).toEqual({ status: 'not_applicable' });
63+
expect(await readCustomizationsFromSqlite(path.join(root, 'absent.db'))).toEqual({ status: 'not_applicable' });
64+
});
65+
66+
it('reads nothing, and never throws, for a selection the dev boot refuses', async () => {
67+
const root = tempProject();
68+
expect(await readLocalCustomizations({ projectRoot: root, env: { OS_DATABASE_DRIVER: 'memory' } }))
69+
.toEqual({ status: 'not_applicable' });
70+
});
71+
});
72+
73+
describe('what the notice says', () => {
74+
it('names each pair, the count, the file and `os package export`', () => {
75+
const lines = localCustomizationNotice({
76+
status: 'found',
77+
filename: '/p/.objectstack/data/objectstack.db',
78+
packages: [
79+
{ id: 'com.example.app.custom', items: 2 },
80+
{ id: 'com.vendor.crm.custom', items: 1 },
81+
],
82+
});
83+
const text = lines.join('\n');
84+
expect(text).toContain('3 Studio customizations');
85+
expect(text).toContain('com.example.app.custom (2)');
86+
expect(text).toContain('com.vendor.crm.custom (1)');
87+
expect(text).toContain('/p/.objectstack/data/objectstack.db');
88+
expect(text).toContain('os package export');
89+
});
90+
91+
it('says it could not check, never that there is nothing, when the read failed', () => {
92+
const text = localCustomizationNotice({ status: 'unknown', filename: '/p/x.db', detail: 'file is not a database' }).join('\n');
93+
expect(text).toContain('Could not check /p/x.db');
94+
expect(text).toContain('file is not a database');
95+
expect(text).toContain('os package export');
96+
});
97+
98+
it('says nothing when there is nothing to leave behind', () => {
99+
expect(localCustomizationNotice({ status: 'none', filename: '/p/x.db' })).toEqual([]);
100+
expect(localCustomizationNotice({ status: 'not_applicable' })).toEqual([]);
101+
});
102+
});
103+
104+
describe('only `os build` says it', () => {
105+
it('turns the notice on for build and leaves it off for compile, which `os dev` runs', () => {
106+
const flag = (C: typeof Compile) => (Reflect.construct(C, [[], {}]) as unknown as { reportsLocalCustomizations: boolean })
107+
.reportsLocalCustomizations;
108+
expect(flag(Build)).toBe(true);
109+
expect(flag(Compile)).toBe(false);
110+
});
111+
});

0 commit comments

Comments
 (0)