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'); }); });