Repository Public
1import fs from 'node:fs';2import os from 'node:os';3import path from 'node:path';4import { describe, expect, it } from 'vitest';5import { loadEnvFile, parseEnvFile } from './env-file.js';67describe('AOS_ENV_FILE loader', () => {8 it('parses KEY=VALUE lines, comments, export prefix and quotes', () => {9 expect(parseEnvFile('# c\nA=1\n export B = "two words"\nC=\'x=y\'\n\nnot a line\nD=\nE=a#b\r\n')).toEqual({ A: '1', B: 'two words', C: 'x=y', D: '', E: 'a#b' });10 });11 it('loads only the named file, never overrides, returns names only', () => {12 const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'aos-env-'));13 try {14 const file = path.join(dir, 'keys.env');15 fs.writeFileSync(file, 'ANTHROPIC_API_KEY=from-file\nOPENAI_API_KEY=from-file\n', { mode: 0o600 });16 const env: NodeJS.ProcessEnv = { AOS_ENV_FILE: file, OPENAI_API_KEY: 'from-env' };17 expect(loadEnvFile(env)).toEqual(['ANTHROPIC_API_KEY']);18 expect(env).toMatchObject({ ANTHROPIC_API_KEY: 'from-file', OPENAI_API_KEY: 'from-env' });19 expect(loadEnvFile({})).toEqual([]);20 expect(() => loadEnvFile({ AOS_ENV_FILE: path.join(dir, 'absent') })).toThrow(/AOS_ENV_FILE cannot be read \(ENOENT\)/);21 } finally {22 fs.rmSync(dir, { recursive: true, force: true });23 }24 });25});26