PORT, PUBLIC_URL and POSTGRES_URL are names half the software in the world uses. Bun auto-loads .env into EVERY process started in the platform directory, so a bare PORT reaches the sidecars, the tools they spawn and anything an agent runs from here — and a bare POSTGRES_URL reaches things with no business holding a database credential. Four parts. officer-setup now writes OFFICERDEV_-prefixed names, with the reasoning in the generated file so whoever opens it later knows why. Every consumer reads `OFFICERDEV_X ?? X` — eleven sites across the databases, the DAV router, officer-url.mjs, os-user-postgres and two scripts. The bare name is the FALLBACK, never the source, which is what makes this safe to ship before every machine has been converted. `bun run fix:env` converts a file in place. Idempotent, preserves comments and `export`, leaves the value untouched including an `=` inside it, and refuses to touch names belonging to other software — prefixing PATH or NODE_ENV does not rename a variable, it deletes it. And it runs on every restart from the server settings screen. There rather than at boot because a restart is the one moment the file can be rewritten and take effect in the same breath: the process about to start is the one that reads it. Non-fatal, since the fallback means the worst case is that nothing changed. gen-index reads either spelling out of the file directly, because it is run by hand as often as by the setup and a bare shell has no .env loaded. Verified by converting this machine's own .env: PORT, PUBLIC_URL and the database all still resolve, gen:index still finds its URL, 1152 pass. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
import { prefixEnvText } from './env-prefix';
|
|
|
|
// This rewrites the file the whole platform reads its database credential from, and it runs on every
|
|
// restart from the settings screen. The tests are about what it must NOT do.
|
|
|
|
describe('prefixing', () => {
|
|
test('renames an ordinary variable', () => {
|
|
expect(prefixEnvText('PORT="9000"').text).toBe('OFFICERDEV_PORT="9000"');
|
|
});
|
|
|
|
test('leaves the value completely alone, including an = inside it', () => {
|
|
const line = 'POSTGRES_URL="postgresql://u:p=a=ss@127.0.0.1:5432/officer"';
|
|
expect(prefixEnvText(line).text).toBe(`OFFICERDEV_${line}`);
|
|
});
|
|
|
|
test('is idempotent — it runs on every restart', () => {
|
|
const once = prefixEnvText('PORT="9000"').text;
|
|
const twice = prefixEnvText(once);
|
|
expect(twice.text).toBe(once);
|
|
expect(twice.changed).toEqual([]);
|
|
expect(twice.alreadyPrefixed).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('what it must not touch', () => {
|
|
test('comments and blank lines survive', () => {
|
|
const text = '# the port\n\nPORT="9000"\n# trailing note\n';
|
|
const out = prefixEnvText(text).text;
|
|
expect(out).toContain('# the port');
|
|
expect(out).toContain('# trailing note');
|
|
expect(out.split('\n').length).toBe(text.split('\n').length);
|
|
});
|
|
|
|
test('a commented-out assignment stays commented out', () => {
|
|
expect(prefixEnvText('# PORT="9000"').text).toBe('# PORT="9000"');
|
|
});
|
|
|
|
test('variables belonging to other software are left alone', () => {
|
|
// Prefixing one of these does not rename a variable, it deletes it: nothing reads OFFICERDEV_PATH.
|
|
for (const key of ['NODE_ENV', 'PATH', 'HOME', 'TZ']) {
|
|
expect(prefixEnvText(`${key}=x`).text).toBe(`${key}=x`);
|
|
}
|
|
});
|
|
|
|
test('an `export` prefix is preserved', () => {
|
|
expect(prefixEnvText('export PORT=9000').text).toBe('export OFFICERDEV_PORT=9000');
|
|
});
|
|
|
|
test('a line that is not an assignment passes through', () => {
|
|
expect(prefixEnvText('this is not an env line').text).toBe('this is not an env line');
|
|
});
|
|
});
|