env-file.test.ts51 lines · main
1import { test } from 'node:test';
2import { strict as assert } from 'node:assert';
3
4import { mergeEnvFile } from './env-file.js';
5
6test('creates a new file with two keys', () => {
7 const out = mergeEnvFile('', {
8 BRIVEN_DEPLOYMENT: 'p_01XYZ',
9 NEXT_PUBLIC_BRIVEN_URL: 'https://api.briven.tech',
10 });
11 assert.match(out, /BRIVEN_DEPLOYMENT="p_01XYZ"/);
12 assert.match(out, /NEXT_PUBLIC_BRIVEN_URL="https:\/\/api\.briven\.tech"/);
13});
14
15test('preserves unrelated user-set keys', () => {
16 const existing = 'DATABASE_URL=postgres://x\nMY_TOKEN=abc\n';
17 const out = mergeEnvFile(existing, { BRIVEN_DEPLOYMENT: 'p_01XYZ' });
18 assert.match(out, /DATABASE_URL=postgres:\/\/x/);
19 assert.match(out, /MY_TOKEN=abc/);
20 assert.match(out, /BRIVEN_DEPLOYMENT="p_01XYZ"/);
21});
22
23test('updates an existing BRIVEN_ key in place', () => {
24 const existing = 'BRIVEN_DEPLOYMENT="p_OLD"\nOTHER=keep\n';
25 const out = mergeEnvFile(existing, { BRIVEN_DEPLOYMENT: 'p_NEW' });
26 assert.match(out, /BRIVEN_DEPLOYMENT="p_NEW"/);
27 assert.doesNotMatch(out, /p_OLD/);
28 assert.match(out, /OTHER=keep/);
29});
30
31test('quotes values that contain spaces or special chars', () => {
32 const out = mergeEnvFile('', { BRIVEN_FOO: 'a b c' });
33 assert.match(out, /BRIVEN_FOO="a b c"/);
34});
35
36test('passes malformed lines through untouched', () => {
37 const existing = '# a comment\n\nWEIRDLINE no equals\nBRIVEN_X=old\n';
38 const out = mergeEnvFile(existing, { BRIVEN_X: 'new' });
39 assert.match(out, /# a comment/);
40 assert.match(out, /WEIRDLINE no equals/);
41 assert.match(out, /BRIVEN_X="new"/);
42});
43
44test('normalizes CRLF input to LF output', () => {
45 const existing = 'DATABASE_URL=postgres://x\r\nBRIVEN_X="old"\r\nOTHER=keep\r\n';
46 const out = mergeEnvFile(existing, { BRIVEN_X: 'new' });
47 assert.doesNotMatch(out, /\r/);
48 assert.match(out, /BRIVEN_X="new"/);
49 assert.match(out, /DATABASE_URL=postgres:\/\/x/);
50 assert.match(out, /OTHER=keep/);
51});