|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 4 | +import { resolve as resolvePath } from 'node:path'; |
| 5 | +import { tmpdir } from 'node:os'; |
| 6 | +import { |
| 7 | + resolveDefaultDataDir, |
| 8 | + isServerlessReadOnlyFs, |
| 9 | + __resetDataDirWarningForTests, |
| 10 | +} from '../src/data-dir.js'; |
| 11 | + |
| 12 | +describe('resolveDefaultDataDir', () => { |
| 13 | + beforeEach(() => { |
| 14 | + vi.restoreAllMocks(); |
| 15 | + __resetDataDirWarningForTests(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('honours OS_DATA_DIR when set', () => { |
| 19 | + const dir = resolveDefaultDataDir({ OS_DATA_DIR: '/custom/path' }); |
| 20 | + expect(dir).toBe(resolvePath('/custom/path')); |
| 21 | + }); |
| 22 | + |
| 23 | + it('OS_DATA_DIR wins over serverless detection', () => { |
| 24 | + const dir = resolveDefaultDataDir({ OS_DATA_DIR: '/custom/path', VERCEL: '1' }); |
| 25 | + expect(dir).toBe(resolvePath('/custom/path')); |
| 26 | + }); |
| 27 | + |
| 28 | + it('defaults to <cwd>/.objectstack/data on a writable filesystem', () => { |
| 29 | + const dir = resolveDefaultDataDir({}); |
| 30 | + expect(dir).toBe(resolvePath(process.cwd(), '.objectstack/data')); |
| 31 | + }); |
| 32 | + |
| 33 | + it('falls back to /tmp when running on Vercel', () => { |
| 34 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 35 | + const dir = resolveDefaultDataDir({ VERCEL: '1' }); |
| 36 | + expect(dir).toBe(resolvePath(tmpdir(), '.objectstack/data')); |
| 37 | + expect(warn).toHaveBeenCalledOnce(); |
| 38 | + warn.mockRestore(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('falls back to /tmp on AWS Lambda', () => { |
| 42 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 43 | + const dir = resolveDefaultDataDir({ AWS_LAMBDA_FUNCTION_NAME: 'my-fn' }); |
| 44 | + expect(dir).toBe(resolvePath(tmpdir(), '.objectstack/data')); |
| 45 | + }); |
| 46 | + |
| 47 | + it('warns only once per process', () => { |
| 48 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 49 | + resolveDefaultDataDir({ VERCEL: '1' }); |
| 50 | + resolveDefaultDataDir({ VERCEL: '1' }); |
| 51 | + resolveDefaultDataDir({ VERCEL: '1' }); |
| 52 | + expect(warn).toHaveBeenCalledOnce(); |
| 53 | + warn.mockRestore(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('OS_READONLY_FS escape hatch triggers tmpdir fallback', () => { |
| 57 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 58 | + const dir = resolveDefaultDataDir({ OS_READONLY_FS: '1' }); |
| 59 | + expect(dir).toBe(resolvePath(tmpdir(), '.objectstack/data')); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('isServerlessReadOnlyFs', () => { |
| 64 | + it('detects Vercel via VERCEL=1', () => { |
| 65 | + expect(isServerlessReadOnlyFs({ VERCEL: '1' })).toBe(true); |
| 66 | + }); |
| 67 | + it('detects AWS Lambda via AWS_LAMBDA_FUNCTION_NAME', () => { |
| 68 | + expect(isServerlessReadOnlyFs({ AWS_LAMBDA_FUNCTION_NAME: 'fn' })).toBe(true); |
| 69 | + }); |
| 70 | + it('detects Netlify via NETLIFY=true', () => { |
| 71 | + expect(isServerlessReadOnlyFs({ NETLIFY: 'true' })).toBe(true); |
| 72 | + }); |
| 73 | + it('returns false for an empty environment', () => { |
| 74 | + expect(isServerlessReadOnlyFs({})).toBe(false); |
| 75 | + }); |
| 76 | + it('respects the OS_READONLY_FS escape hatch', () => { |
| 77 | + expect(isServerlessReadOnlyFs({ OS_READONLY_FS: '1' })).toBe(true); |
| 78 | + expect(isServerlessReadOnlyFs({ OS_READONLY_FS: 'true' })).toBe(true); |
| 79 | + expect(isServerlessReadOnlyFs({ OS_READONLY_FS: '0' })).toBe(false); |
| 80 | + }); |
| 81 | +}); |
0 commit comments