|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #31488 — which database `os start`'s ownership refusal must name, and the |
| 5 | + * channel that carries it to the `serve` child. |
| 6 | + * |
| 7 | + * The ceremony (`os migrate organization-ownership`) resolves its database the |
| 8 | + * way every CLI command does: `--database-url`, then `$OS_DATABASE_URL`, then |
| 9 | + * the project's declared datasource, then `<state dir>/data/objectstack.db`, |
| 10 | + * where the state dir is `$OS_HOME` or the project's `.objectstack`. `os start` |
| 11 | + * resolves the same ladder with two inputs the ceremony never sees: its |
| 12 | + * `-d` flag and its `--home` flag. A database reached through either is one |
| 13 | + * the ceremony, run bare in the same shell, does not find. |
| 14 | + * |
| 15 | + * - ⭐ `-d` → named; `--home` with the home's file → named; |
| 16 | + * - `--home` beside `$OS_DATABASE_URL` or a declared datasource → not named |
| 17 | + * (the ceremony reads those too); no flag → not named (the default and an |
| 18 | + * exported `$OS_HOME` reach the same file); |
| 19 | + * - the channel is owned by the parent: set when named, deleted otherwise. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, expect, it } from 'vitest'; |
| 23 | +import { |
| 24 | + INTERNAL_CEREMONY_DATABASE_URL_ENV, |
| 25 | + ceremonyDatabaseUrl, |
| 26 | + readCeremonyDatabaseChannel, |
| 27 | + writeCeremonyDatabaseChannel, |
| 28 | +} from './internal-ceremony-database-channel.js'; |
| 29 | + |
| 30 | +const HOME_FILE = 'file:/srv/os/home/data/objectstack.db'; |
| 31 | + |
| 32 | +describe('ceremonyDatabaseUrl — the database the refusal names, or none', () => { |
| 33 | + it('⭐ a database from `-d` is named, whatever the home', () => { |
| 34 | + expect(ceremonyDatabaseUrl({ url: 'file:A.db', source: 'explicit' }, { homeFlag: undefined })).toBe('file:A.db'); |
| 35 | + expect(ceremonyDatabaseUrl({ url: 'file:A.db', source: 'explicit' }, { homeFlag: '/srv/os/home' })).toBe('file:A.db'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('⭐ the file of a `--home` home is named', () => { |
| 39 | + expect(ceremonyDatabaseUrl({ url: HOME_FILE, source: 'unified-default' }, { homeFlag: '/srv/os/home' })).toBe(HOME_FILE); |
| 40 | + expect(ceremonyDatabaseUrl({ url: 'file:/srv/os/home/data/dev.db', source: 'legacy-file' }, { homeFlag: '/srv/os/home' })) |
| 41 | + .toBe('file:/srv/os/home/data/dev.db'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('a database the ceremony reaches by its own ladder is not named', () => { |
| 45 | + // `$OS_DATABASE_URL` / a declared datasource: the ceremony reads them too. |
| 46 | + expect(ceremonyDatabaseUrl({ url: 'postgres://db/os', source: 'env' }, { homeFlag: '/srv/os/home' })).toBeUndefined(); |
| 47 | + expect(ceremonyDatabaseUrl({ url: 'file:/app/data.db', source: 'config-datasource' }, { homeFlag: '/srv/os/home' })) |
| 48 | + .toBeUndefined(); |
| 49 | + // No flag: the project's `.objectstack`, or an exported `$OS_HOME`, is the ceremony's default too. |
| 50 | + expect(ceremonyDatabaseUrl({ url: HOME_FILE, source: 'unified-default' }, { homeFlag: undefined })).toBeUndefined(); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('the ceremony-database channel to the serve child', () => { |
| 55 | + it('is set when there is a database to name, and deleted otherwise — an inherited copy never speaks', () => { |
| 56 | + const env: NodeJS.ProcessEnv = {}; |
| 57 | + writeCeremonyDatabaseChannel(env, 'file:A.db'); |
| 58 | + expect(env[INTERNAL_CEREMONY_DATABASE_URL_ENV]).toBe('file:A.db'); |
| 59 | + expect(readCeremonyDatabaseChannel(env)).toBe('file:A.db'); |
| 60 | + writeCeremonyDatabaseChannel(env, undefined); |
| 61 | + expect(INTERNAL_CEREMONY_DATABASE_URL_ENV in env).toBe(false); |
| 62 | + expect(readCeremonyDatabaseChannel(env)).toBeUndefined(); |
| 63 | + expect(readCeremonyDatabaseChannel({ [INTERNAL_CEREMONY_DATABASE_URL_ENV]: '' })).toBeUndefined(); |
| 64 | + }); |
| 65 | +}); |
0 commit comments