From b978b6fa9ded051d259ff42485143083198db671 Mon Sep 17 00:00:00 2001 From: Nystik <236107-Nystik@users.noreply.gitlab.com> Date: Fri, 15 May 2026 18:00:56 +0200 Subject: [PATCH] add tests for the transform hooks --- src/shims/fs/transforms.js | 7 +++ src/shims/fs/transforms.test.js | 80 +++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/shims/fs/transforms.test.js diff --git a/src/shims/fs/transforms.js b/src/shims/fs/transforms.js index 42e754d..9a04365 100644 --- a/src/shims/fs/transforms.js +++ b/src/shims/fs/transforms.js @@ -87,3 +87,10 @@ export function applyWriteTransform(path, data) { return data; } } + +// Test-only: clear all registered hooks. +export function _reset() { + pathResolvers.length = 0; + readTransforms.clear(); + writeTransforms.clear(); +} diff --git a/src/shims/fs/transforms.test.js b/src/shims/fs/transforms.test.js new file mode 100644 index 0000000..9831e7a --- /dev/null +++ b/src/shims/fs/transforms.test.js @@ -0,0 +1,80 @@ +import { describe, it, expect, beforeEach } from "vitest"; +import { + registerPathResolver, + resolvePath, + registerReadTransform, + applyReadTransform, + registerWriteTransform, + applyWriteTransform, + _reset, +} from "./transforms.js"; + +beforeEach(() => { + _reset(); +}); + +describe("resolvePath", () => { + it("returns the resolved path when a matcher hits", () => { + registerPathResolver( + (p) => p === ".obsidian/workspace.json", + () => ".obsidian/workspace.default.json", + ); + + expect(resolvePath(".obsidian/workspace.json")).toBe( + ".obsidian/workspace.default.json", + ); + }); + + it("returns the input path when no matcher hits", () => { + registerPathResolver( + (p) => p === ".obsidian/workspace.json", + () => ".obsidian/workspace.default.json", + ); + + expect(resolvePath("notes/foo.md")).toBe("notes/foo.md"); + }); +}); + +describe("applyReadTransform", () => { + it("applies a registered transform when the path matches", () => { + registerReadTransform(".obsidian/core-plugins.json", (data) => + data.replace('"sync":true', '"sync":false'), + ); + + const input = '{"sync":true,"foo":1}'; + expect(applyReadTransform(".obsidian/core-plugins.json", input)).toBe( + '{"sync":false,"foo":1}', + ); + }); + + it("returns data unchanged when no transform is registered for the path", () => { + registerReadTransform(".obsidian/core-plugins.json", (data) => + data.replace('"sync":true', '"sync":false'), + ); + + const input = '{"sync":true}'; + expect(applyReadTransform("notes/foo.md", input)).toBe(input); + }); +}); + +describe("applyWriteTransform", () => { + it("applies a registered transform when the path matches", () => { + registerWriteTransform(".obsidian/workspaces.json", (data) => + data.replace('"active":"w2"', '"active":"default"'), + ); + + const input = '{"active":"w2"}'; + expect(applyWriteTransform(".obsidian/workspaces.json", input)).toBe( + '{"active":"default"}', + ); + }); +}); + +describe("path normalization", () => { + it("matches a path registered with one separator form against lookups in another", () => { + registerReadTransform(".obsidian/foo.json", (data) => `seen:${data}`); + + expect(applyReadTransform(".obsidian\\foo.json", "x")).toBe("seen:x"); + expect(applyReadTransform("/.obsidian/foo.json", "x")).toBe("seen:x"); + }); +});