|
| 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