From 4c955886ffffe780e9cbc65e3e53c427f3e30430 Mon Sep 17 00:00:00 2001 From: Karstein Phobic Nyvold Kvistad Date: Wed, 29 Apr 2026 10:03:32 +0200 Subject: [PATCH] Revert "feat(live-values): atomic writer for tui-live-values.json" This reverts commit c565888ff5534689b06bd3182d208d5dc8779948. --- src/live-values-write.ts | 41 ------------------------ tests/unit/live-values-write.test.ts | 48 ---------------------------- 2 files changed, 89 deletions(-) delete mode 100644 src/live-values-write.ts delete mode 100644 tests/unit/live-values-write.test.ts diff --git a/src/live-values-write.ts b/src/live-values-write.ts deleted file mode 100644 index 2ce23d5..0000000 --- a/src/live-values-write.ts +++ /dev/null @@ -1,41 +0,0 @@ -import * as fs from 'fs/promises'; -import * as path from 'path'; - -export interface LiveValueSnapshotIn { - value: string; - type?: string; - ts: number; -} - -export interface LiveValuesPayloadIn { - device: string; - pou_name: string; - values: Record; -} - -/** - * Server-side counterpart to the TUI's readLiveValues. - * - * Wraps the caller's payload in the v1 envelope, writes atomically via - * `..tmp` + rename, and creates parent dirs as needed. Mirrors - * src/tui/shared/state-write.ts (the selection writer); kept separate - * because that one's ESM and the server is CJS. - */ -export async function writeLiveValues( - filePath: string, - projectDir: string, - payload: LiveValuesPayloadIn -): Promise { - await fs.mkdir(path.dirname(filePath), { recursive: true }); - const envelope = { - version: 1, - updated_at: new Date().toISOString(), - project_dir: projectDir, - device: payload.device, - pou_name: payload.pou_name, - values: payload.values, - }; - const tmp = `${filePath}.${process.pid}.tmp`; - await fs.writeFile(tmp, JSON.stringify(envelope, null, 2), 'utf8'); - await fs.rename(tmp, filePath); -} diff --git a/tests/unit/live-values-write.test.ts b/tests/unit/live-values-write.test.ts deleted file mode 100644 index db630f1..0000000 --- a/tests/unit/live-values-write.test.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { describe, it, expect } from 'vitest'; -import * as fs from 'fs/promises'; -import * as path from 'path'; -import * as os from 'os'; -import { writeLiveValues } from '../../src/live-values-write'; - -async function tmpDir(): Promise { - return fs.mkdtemp(path.join(os.tmpdir(), 'phobics-lvw-')); -} - -const sample = { - device: 'D1', - pou_name: 'PLC_PRG', - values: { - counter: { value: '47', type: 'INT', ts: Date.now() }, - }, -}; - -describe('writeLiveValues', () => { - it('writes the v1 envelope with required fields', async () => { - const dir = await tmpDir(); - const target = path.join(dir, 'tui-live-values.json'); - await writeLiveValues(target, '/abs/project', sample); - - const parsed = JSON.parse(await fs.readFile(target, 'utf8')); - expect(parsed.version).toBe(1); - expect(parsed.project_dir).toBe('/abs/project'); - expect(parsed.device).toBe('D1'); - expect(parsed.pou_name).toBe('PLC_PRG'); - expect(parsed.values.counter.value).toBe('47'); - expect(typeof parsed.updated_at).toBe('string'); - }); - - it('creates parent dirs as needed', async () => { - const dir = await tmpDir(); - const target = path.join(dir, 'a', 'b', 'tui-live-values.json'); - await writeLiveValues(target, '/abs/project', sample); - expect((await fs.stat(target)).isFile()).toBe(true); - }); - - it('does not leave .tmp residue on success', async () => { - const dir = await tmpDir(); - const target = path.join(dir, 'tui-live-values.json'); - await writeLiveValues(target, '/abs/project', sample); - const entries = await fs.readdir(dir); - expect(entries.filter((e) => e.endsWith('.tmp'))).toEqual([]); - }); -});