0
0
Fork 0
Codesys-MCP-SP21-plus/tests/tui/state-paths.test.ts
Karstein Phobic Nyvold Kvistad 461bd10a73 tui: add state-file path resolver (Windows + XDG)
Resolves the location of the cross-process TUI state file:
- Windows: %LOCALAPPDATA%/codesys-mcp/tui-state.json
- POSIX: $XDG_STATE_HOME/codesys-mcp/tui-state.json, falling back to
  $HOME/.local/state/codesys-mcp/tui-state.json

Uses path.win32 / path.posix explicitly so the POSIX branches still
produce POSIX-flavored paths when this runs on a Windows host (test
machine), keeping the unit tests host-independent.
2026-04-28 21:50:44 +02:00

51 lines
1.6 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import * as path from 'path';
import { stateFilePath } from '../../src/tui/shared/state-paths.ts';
const ORIG_PLATFORM = Object.getOwnPropertyDescriptor(process, 'platform')!;
const ORIG_ENV = { ...process.env };
function setPlatform(p: NodeJS.Platform) {
Object.defineProperty(process, 'platform', { value: p, configurable: true });
}
beforeEach(() => {
process.env = { ...ORIG_ENV };
});
afterEach(() => {
Object.defineProperty(process, 'platform', ORIG_PLATFORM);
process.env = { ...ORIG_ENV };
});
describe('stateFilePath', () => {
it('uses %LOCALAPPDATA%/codesys-mcp/tui-state.json on Windows', () => {
setPlatform('win32');
process.env.LOCALAPPDATA = 'C:\\\\Users\\\\u\\\\AppData\\\\Local';
const p = stateFilePath();
expect(p).toBe(
path.join('C:\\\\Users\\\\u\\\\AppData\\\\Local', 'codesys-mcp', 'tui-state.json')
);
});
it('uses $XDG_STATE_HOME/codesys-mcp/tui-state.json when set', () => {
setPlatform('linux');
process.env.XDG_STATE_HOME = '/tmp/xdg-state';
const p = stateFilePath();
expect(p).toBe('/tmp/xdg-state/codesys-mcp/tui-state.json');
});
it('falls back to ~/.local/state on Linux without XDG_STATE_HOME', () => {
setPlatform('linux');
delete process.env.XDG_STATE_HOME;
process.env.HOME = '/home/u';
const p = stateFilePath();
expect(p).toBe('/home/u/.local/state/codesys-mcp/tui-state.json');
});
it('throws on Windows when LOCALAPPDATA is unset', () => {
setPlatform('win32');
delete process.env.LOCALAPPDATA;
expect(() => stateFilePath()).toThrow(/LOCALAPPDATA/);
});
});