test(git-sync): add reviewer-requested coverage across engine, server, client

Implements the test cases called out in the PR #119 review threads
(code-review, test-strategy report, red-team) — TESTS ONLY, no production
code changes.

packages/git-sync (vitest):
- lib converter/markdown gaps: pageBreak data-loss (it.fails repro),
  subpages lossy round-trip, nested/fenced callouts, ol->taskList bridge,
  column.width number<->string drift, empty details.
- engine units: parentFolderFile, planReconciliation swap/chained move,
  buildVaultLayout last-resort-by-id, firstDivergence, applyPushActions /
  applyPullActions failure isolation.
- real temp-git integration: diffNameStatus -z rename+add/modify
  alignment, copy-line behavior, per-invocation committer identity (no
  leak into repo/global config).
- ENFORCED type-level GitSyncClient contract via vitest typecheck over a
  *.test-d.ts file (tsconfig.vitest.json; build tsconfig untouched).

apps/server (jest):
- orchestrator: delete-cap neutralization + fail-safe, Redis lock / mutex
  skip ladder + release-on-throw, merge guard, pull/push order, remote
  template substitution, poll lifecycle.
- page-change listener: loop-guard, debounce coalescing, id resolution,
  error swallowing.
- vault registry, controller authz (trigger + status), env
  validation/getters, page.service git-sync provenance stamping,
  persistence precedence (agent > git-sync > user) + no boundary snapshot,
  space.service audit-delta, space.repo jsonb-merge, converter-gate corpus
  extension (mention/math/details/marks).

apps/client (vitest + testing-library):
- history-item git-sync badge: render gating + non-clickable.
- edit-space-form toggle: initial state, optimistic payload, rollback on
  error, disabled states.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
claude_code
2026-06-21 17:51:35 +03:00
committed by claude code agent 227
parent eb0aa12c83
commit ba15fde809
20 changed files with 3331 additions and 0 deletions

View File

@@ -0,0 +1,115 @@
// Unit tests for the ops/testing controller (plan §6). The orchestrator, env,
// and the workspace-ability factory are hand-built mocks. We assert the admin
// guard (non-admin -> ForbiddenException, no orchestrator call), that trigger
// uses the workspace from request context (never the body), and that status
// returns the env-derived object.
import { ForbiddenException } from '@nestjs/common';
import {
WorkspaceCaslAction,
WorkspaceCaslSubject,
} from '../../core/casl/interfaces/workspace-ability.type';
import { GitSyncController } from './git-sync.controller';
type AnyMock = jest.Mock;
interface Built {
controller: GitSyncController;
orchestrator: { runOnce: AnyMock };
env: Record<string, AnyMock>;
workspaceAbility: { createForUser: AnyMock };
ability: { cannot: AnyMock };
}
function build(opts: { cannot?: boolean } = {}): Built {
const { cannot = false } = opts;
const ability = { cannot: jest.fn(() => cannot) };
const workspaceAbility = { createForUser: jest.fn(() => ability) };
const orchestrator = {
runOnce: jest.fn(async () => ({ spaceId: 'space-1', ran: true })),
};
const env: Record<string, AnyMock> = {
isGitSyncEnabled: jest.fn(() => true),
getGitSyncDataDir: jest.fn(() => '/vaults'),
getGitSyncPollIntervalMs: jest.fn(() => 15000),
getGitSyncDebounceMs: jest.fn(() => 2000),
getGitSyncServiceUserId: jest.fn(() => 'svc-user'),
};
const controller = new GitSyncController(
orchestrator as any,
env as any,
workspaceAbility as any,
);
return { controller, orchestrator, env, workspaceAbility, ability };
}
const USER = { id: 'user-1' } as any;
const WORKSPACE = { id: 'ctx-ws' } as any;
beforeEach(() => {
jest.clearAllMocks();
});
describe('GitSyncController', () => {
describe('trigger', () => {
it('blocks a non-admin: throws ForbiddenException and never calls runOnce', async () => {
const { controller, orchestrator, ability } = build({ cannot: true });
await expect(
controller.trigger({ spaceId: 'space-1' } as any, USER, WORKSPACE),
).rejects.toBeInstanceOf(ForbiddenException);
expect(ability.cannot).toHaveBeenCalledWith(
WorkspaceCaslAction.Manage,
WorkspaceCaslSubject.Settings,
);
expect(orchestrator.runOnce).not.toHaveBeenCalled();
});
it('admin: calls runOnce(dto.spaceId, workspace.id) using the workspace from context', async () => {
const { controller, orchestrator } = build({ cannot: false });
// The body carries an attacker-controlled workspaceId that must be ignored.
const res = await controller.trigger(
{ spaceId: 'space-1', workspaceId: 'evil-ws' } as any,
USER,
WORKSPACE,
);
expect(orchestrator.runOnce).toHaveBeenCalledWith('space-1', 'ctx-ws');
expect(res).toEqual({ spaceId: 'space-1', ran: true });
});
});
describe('status', () => {
it('blocks a non-admin: throws ForbiddenException and never reads env', async () => {
const { controller, env, ability } = build({ cannot: true });
await expect(controller.status(USER, WORKSPACE)).rejects.toBeInstanceOf(
ForbiddenException,
);
expect(ability.cannot).toHaveBeenCalledWith(
WorkspaceCaslAction.Manage,
WorkspaceCaslSubject.Settings,
);
// The admin guard short-circuits before the env-derived status is built.
expect(env.isGitSyncEnabled).not.toHaveBeenCalled();
});
it('admin: returns the env-derived status object', async () => {
const { controller } = build({ cannot: false });
const res = await controller.status(USER, WORKSPACE);
expect(res).toEqual({
enabled: true,
dataDir: '/vaults',
pollIntervalMs: 15000,
debounceMs: 2000,
serviceUserConfigured: true,
});
});
});
});

View File

@@ -0,0 +1,220 @@
// Unit tests for the event-driven git-sync trigger (plan §10). The orchestrator
// and page repo are hand-built mocks; the debounce coalescing is exercised with
// jest fake timers. We assert the gate, the loop-guard (anti-echo), the
// missing-page short-circuit, the heterogeneous event-shape id resolution, the
// debounce collapse, and that errors are swallowed + logged.
import { Logger } from '@nestjs/common';
import { PageChangeListener } from './page-change.listener';
type AnyMock = jest.Mock;
interface Built {
listener: PageChangeListener;
env: { isGitSyncEnabled: AnyMock; getGitSyncDebounceMs: AnyMock };
orchestrator: { runOnce: AnyMock };
pageRepo: { findById: AnyMock };
}
function build(opts: { enabled?: boolean; debounceMs?: number } = {}): Built {
const { enabled = true, debounceMs = 2000 } = opts;
const env = {
isGitSyncEnabled: jest.fn(() => enabled),
getGitSyncDebounceMs: jest.fn(() => debounceMs),
};
const orchestrator = { runOnce: jest.fn(async () => undefined) };
const pageRepo = { findById: jest.fn() };
const listener = new PageChangeListener(
env as any,
orchestrator as any,
pageRepo as any,
);
return { listener, env, orchestrator, pageRepo };
}
beforeEach(() => {
jest.clearAllMocks();
});
describe('PageChangeListener', () => {
describe('gate', () => {
it('does nothing when git-sync is disabled (no findById, no schedule)', async () => {
const { listener, orchestrator, pageRepo } = build({ enabled: false });
await listener.handlePageEvent({ pageId: 'p1', workspaceId: 'ws-1' });
expect(pageRepo.findById).not.toHaveBeenCalled();
expect(orchestrator.runOnce).not.toHaveBeenCalled();
});
});
describe('loop-guard (anti-echo)', () => {
it("does NOT schedule a cycle when the page row's source is 'git-sync'", async () => {
jest.useFakeTimers();
try {
const { listener, orchestrator, pageRepo } = build();
pageRepo.findById.mockResolvedValue({
id: 'p1',
spaceId: 'space-1',
workspaceId: 'ws-1',
lastUpdatedSource: 'git-sync',
});
await listener.handlePageEvent({ pageId: 'p1', workspaceId: 'ws-1' });
jest.runOnlyPendingTimers();
expect(orchestrator.runOnce).not.toHaveBeenCalled();
} finally {
jest.useRealTimers();
}
});
it('schedules exactly one cycle for a normal (non-git-sync) source', async () => {
jest.useFakeTimers();
try {
const { listener, orchestrator, pageRepo } = build();
pageRepo.findById.mockResolvedValue({
id: 'p1',
spaceId: 'space-1',
workspaceId: 'ws-1',
lastUpdatedSource: 'user',
});
await listener.handlePageEvent({ pageId: 'p1', workspaceId: 'ws-1' });
jest.runOnlyPendingTimers();
expect(orchestrator.runOnce).toHaveBeenCalledTimes(1);
expect(orchestrator.runOnce).toHaveBeenCalledWith('space-1', 'ws-1');
} finally {
jest.useRealTimers();
}
});
});
describe('missing page', () => {
it('does not schedule when findById returns null/undefined', async () => {
jest.useFakeTimers();
try {
const { listener, orchestrator, pageRepo } = build();
pageRepo.findById.mockResolvedValue(undefined);
await listener.handlePageEvent({ pageId: 'p1', workspaceId: 'ws-1' });
jest.runOnlyPendingTimers();
expect(orchestrator.runOnce).not.toHaveBeenCalled();
} finally {
jest.useRealTimers();
}
});
});
describe('spaceId/workspaceId resolution', () => {
// The page row used to fill in any ids the event omits.
const pageRow = {
id: 'p1',
spaceId: 'row-space',
workspaceId: 'row-ws',
lastUpdatedSource: 'user',
};
async function resolve(event: Record<string, unknown>) {
jest.useFakeTimers();
try {
const { listener, orchestrator, pageRepo } = build();
pageRepo.findById.mockResolvedValue(pageRow);
await listener.handlePageEvent(event as any);
jest.runOnlyPendingTimers();
return { orchestrator, pageRepo };
} finally {
jest.useRealTimers();
}
}
it("resolves pageId + event.spaceId + event.workspaceId", async () => {
const { orchestrator, pageRepo } = await resolve({
pageId: 'p1',
spaceId: 'evt-space',
workspaceId: 'evt-ws',
});
expect(pageRepo.findById).toHaveBeenCalledWith('p1', { includeContent: false });
expect(orchestrator.runOnce).toHaveBeenCalledWith('evt-space', 'evt-ws');
});
it('resolves pageId from pageIds[0]', async () => {
const { orchestrator, pageRepo } = await resolve({
pageIds: ['p1', 'p2'],
spaceId: 'evt-space',
workspaceId: 'evt-ws',
});
expect(pageRepo.findById).toHaveBeenCalledWith('p1', { includeContent: false });
expect(orchestrator.runOnce).toHaveBeenCalledWith('evt-space', 'evt-ws');
});
it('resolves pageId + spaceId from pages[]', async () => {
const { orchestrator } = await resolve({
pages: [{ id: 'p1', spaceId: 'pages-space' }],
workspaceId: 'evt-ws',
});
expect(orchestrator.runOnce).toHaveBeenCalledWith('pages-space', 'evt-ws');
});
it('resolves pageId + spaceId from node', async () => {
const { orchestrator } = await resolve({
node: { id: 'p1', spaceId: 'node-space' },
workspaceId: 'evt-ws',
});
expect(orchestrator.runOnce).toHaveBeenCalledWith('node-space', 'evt-ws');
});
it('falls back to the fetched page row when the event omits spaceId/workspaceId', async () => {
const { orchestrator } = await resolve({ pageId: 'p1' });
// No spaceId/workspaceId on the event -> use the page row's values.
expect(orchestrator.runOnce).toHaveBeenCalledWith('row-space', 'row-ws');
});
});
describe('debounce coalescing', () => {
it('collapses a burst of N events for one space into exactly one runOnce', async () => {
jest.useFakeTimers();
try {
const { listener, orchestrator, pageRepo } = build({ debounceMs: 500 });
pageRepo.findById.mockResolvedValue({
id: 'p1',
spaceId: 'space-1',
workspaceId: 'ws-1',
lastUpdatedSource: 'user',
});
// Fire a burst of 5 events; await each so its findById promise settles
// and schedule() runs before the next event resets the timer.
for (let i = 0; i < 5; i++) {
await listener.handlePageEvent({ pageId: 'p1', workspaceId: 'ws-1' });
}
// Nothing fired yet (still within the debounce window).
expect(orchestrator.runOnce).not.toHaveBeenCalled();
// Advance past the debounce window: the coalesced cycle fires once.
jest.advanceTimersByTime(500);
expect(orchestrator.runOnce).toHaveBeenCalledTimes(1);
expect(orchestrator.runOnce).toHaveBeenCalledWith('space-1', 'ws-1');
} finally {
jest.useRealTimers();
}
});
});
describe('error swallowing', () => {
it('does not throw and logs a warning when findById throws', async () => {
const warnSpy = jest
.spyOn(Logger.prototype, 'warn')
.mockImplementation(() => undefined);
try {
const { listener, orchestrator, pageRepo } = build();
pageRepo.findById.mockRejectedValue(new Error('db down'));
await expect(
listener.handlePageEvent({ pageId: 'p1', workspaceId: 'ws-1' }),
).resolves.toBeUndefined();
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(String(warnSpy.mock.calls[0][0])).toContain('db down');
expect(orchestrator.runOnce).not.toHaveBeenCalled();
} finally {
warnSpy.mockRestore();
}
});
});
});

View File

@@ -0,0 +1,397 @@
// Unit tests for the git-sync control plane (plan §9/§10/§11). The vendored
// engine (@docmost/git-sync) is fully mocked so we exercise ONLY the
// orchestrator's wiring: gating, the Redis leader lock + in-process mutex,
// the pull/push call order, the delete-cap anti-data-loss guard, the remote
// template substitution, and the idempotent interval lifecycle.
//
// The engine mock must be declared before importing the orchestrator so the
// module-graph import binds to the mocked functions (same idiom as the
// datasource spec's top-of-file jest.mock stubs that avoid the React graph).
jest.mock('@docmost/git-sync', () => ({
readExisting: jest.fn(),
computePullActions: jest.fn(),
applyPullActions: jest.fn(),
runPush: jest.fn(),
}));
import { Logger } from '@nestjs/common';
import {
readExisting,
computePullActions,
applyPullActions,
runPush,
} from '@docmost/git-sync';
import { GitSyncOrchestrator } from './git-sync.orchestrator';
type AnyMock = jest.Mock;
const readExistingMock = readExisting as unknown as AnyMock;
const computePullActionsMock = computePullActions as unknown as AnyMock;
const applyPullActionsMock = applyPullActions as unknown as AnyMock;
const runPushMock = runPush as unknown as AnyMock;
interface BuildOptions {
/** Env tunables (only the load-bearing ones are surfaced as overrides). */
enabled?: boolean;
serviceUserId?: string | undefined;
maxDeletes?: number;
remoteTemplate?: string | undefined;
dataDir?: string;
pollIntervalMs?: number;
debounceMs?: number;
/** A hook applied to the fake vault so a test can override its behaviour. */
vaultOverrides?: Record<string, unknown>;
}
interface Built {
orchestrator: GitSyncOrchestrator;
env: Record<string, AnyMock>;
dataSource: { bind: AnyMock };
client: Record<string, AnyMock>;
vaultRegistry: { getVault: AnyMock; vaultPath: AnyMock };
vault: Record<string, AnyMock>;
scheduler: Record<string, AnyMock>;
redis: { set: AnyMock; eval: AnyMock };
redisService: { getOrThrow: AnyMock };
db: unknown;
}
function build(opts: BuildOptions = {}): Built {
const {
enabled = true,
maxDeletes = 100,
remoteTemplate = undefined,
dataDir = '/vaults',
pollIntervalMs = 15000,
debounceMs = 2000,
vaultOverrides = {},
} = opts;
// Distinguish "key omitted" (default to a valid id) from "key present but
// undefined" (the no-service-user test deliberately sets it undefined).
const serviceUserId = 'serviceUserId' in opts ? opts.serviceUserId : 'svc-user';
const env: Record<string, AnyMock> = {
isGitSyncEnabled: jest.fn(() => enabled),
getGitSyncServiceUserId: jest.fn(() => serviceUserId),
getGitSyncMaxDeletesPerCycle: jest.fn(() => maxDeletes),
getGitSyncRemoteTemplate: jest.fn(() => remoteTemplate),
getGitSyncDataDir: jest.fn(() => dataDir),
getGitSyncPollIntervalMs: jest.fn(() => pollIntervalMs),
getGitSyncDebounceMs: jest.fn(() => debounceMs),
};
// The read-side / write-side client the datasource hands back.
const client: Record<string, AnyMock> = {
listSpaceTree: jest.fn(async () => ({ pages: [], complete: true })),
deletePage: jest.fn(async () => undefined),
createPage: jest.fn(async () => undefined),
updatePageBody: jest.fn(async () => undefined),
};
const dataSource = { bind: jest.fn(() => client) };
// The fake VaultGit: every method the orchestrator calls is a jest.fn.
const vault: Record<string, AnyMock> = {
assertGitAvailable: jest.fn(async () => undefined),
ensureRepo: jest.fn(async () => undefined),
isMergeInProgress: jest.fn(async () => false),
ensureBranch: jest.fn(async () => undefined),
checkout: jest.fn(async () => undefined),
listTrackedFiles: jest.fn(async () => []),
...(vaultOverrides as Record<string, AnyMock>),
};
const vaultRegistry = {
getVault: jest.fn(async () => vault),
vaultPath: jest.fn((spaceId: string) => `${dataDir}/${spaceId}`),
};
const scheduler: Record<string, AnyMock> = {
addInterval: jest.fn(),
deleteInterval: jest.fn(),
};
const redis = {
// Default: lock acquired. Tests override per-case.
set: jest.fn(async () => 'OK'),
eval: jest.fn(async () => 1),
};
const redisService = { getOrThrow: jest.fn(() => redis) };
const db = {};
const orchestrator = new GitSyncOrchestrator(
env as any,
dataSource as any,
vaultRegistry as any,
scheduler as any,
redisService as any,
db as any,
);
return {
orchestrator,
env,
dataSource,
client,
vaultRegistry,
vault,
scheduler,
redis,
redisService,
db,
};
}
/** Reasonable engine defaults so a happy-path driveCycle completes. */
function primeEngineHappyPath(): void {
readExistingMock.mockResolvedValue({});
computePullActionsMock.mockReturnValue({ creates: [], updates: [], deletes: [] });
applyPullActionsMock.mockResolvedValue({
written: 0,
deleted: 0,
merge: { conflict: false },
});
runPushMock.mockResolvedValue({ mode: 'apply', failures: [], planned: { deletes: 0 } });
}
beforeEach(() => {
jest.clearAllMocks();
primeEngineHappyPath();
});
describe('GitSyncOrchestrator', () => {
describe('runOnce gating', () => {
it("short-circuits with skipped:'disabled' when git-sync is disabled", async () => {
const { orchestrator, redis, vaultRegistry } = build({ enabled: false });
const res = await orchestrator.runOnce('space-1', 'ws-1');
expect(res).toEqual({ spaceId: 'space-1', ran: false, skipped: 'disabled' });
// No lock, no vault work performed.
expect(redis.set).not.toHaveBeenCalled();
expect(vaultRegistry.getVault).not.toHaveBeenCalled();
});
it("returns skipped:'no-service-user' when the service user id is falsy", async () => {
const { orchestrator, redis } = build({ serviceUserId: undefined });
const res = await orchestrator.runOnce('space-1', 'ws-1');
expect(res).toEqual({
spaceId: 'space-1',
ran: false,
skipped: 'no-service-user',
});
expect(redis.set).not.toHaveBeenCalled();
});
});
describe('in-process mutex', () => {
it("a second runOnce while the first is in-flight returns skipped:'in-progress'", async () => {
const built = build();
let release!: () => void;
const gate = new Promise<void>((resolve) => {
release = resolve;
});
// Hang the first cycle inside driveCycle by stalling getVault.
built.vaultRegistry.getVault.mockImplementationOnce(async () => {
await gate;
return built.vault;
});
const first = built.orchestrator.runOnce('space-1', 'ws-1');
// Let the first call enter the running set + acquire the lock.
await Promise.resolve();
await Promise.resolve();
const second = await built.orchestrator.runOnce('space-1', 'ws-1');
expect(second).toEqual({
spaceId: 'space-1',
ran: false,
skipped: 'in-progress',
});
release();
await first;
});
});
describe('redis leader lock', () => {
it("returns skipped:'lock-held' and cleans up the mutex when the lock is not acquired", async () => {
const built = build();
// First acquire fails (not 'OK'); a later acquire succeeds.
built.redis.set
.mockResolvedValueOnce(null)
.mockResolvedValue('OK');
const res = await built.orchestrator.runOnce('space-1', 'ws-1');
expect(res).toEqual({
spaceId: 'space-1',
ran: false,
skipped: 'lock-held',
});
// The mutex must be clear: a subsequent call can acquire + run.
const res2 = await built.orchestrator.runOnce('space-1', 'ws-1');
expect(res2.ran).toBe(true);
expect(res2.skipped).toBeUndefined();
});
});
describe('poisoned-space protection', () => {
it('releases the lock and clears the mutex when driveCycle throws, returning { error }', async () => {
const built = build();
jest.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);
// Make the real apply runPush reject; dry-run still resolves first.
runPushMock
.mockResolvedValueOnce({ mode: 'apply', failures: [], planned: { deletes: 0 } })
.mockRejectedValueOnce(new Error('boom'));
const res = await built.orchestrator.runOnce('space-1', 'ws-1');
expect(res.ran).toBe(false);
expect(res.error).toBe('boom');
// CAS release was invoked (eval) and the space is no longer "running":
expect(built.redis.eval).toHaveBeenCalledTimes(1);
// A subsequent call can re-acquire (mutex cleared after the throw).
runPushMock.mockResolvedValue({ mode: 'apply', failures: [], planned: { deletes: 0 } });
const res2 = await built.orchestrator.runOnce('space-1', 'ws-1');
expect(res2.ran).toBe(true);
});
});
describe('merge-in-progress guard', () => {
it("returns skipped:'merge-in-progress' and runs no pull/push", async () => {
jest.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined);
const built = build({ vaultOverrides: { isMergeInProgress: jest.fn(async () => true) } });
const res = await built.orchestrator.runOnce('space-1', 'ws-1');
expect(res).toEqual({
spaceId: 'space-1',
ran: false,
skipped: 'merge-in-progress',
});
expect(applyPullActionsMock).not.toHaveBeenCalled();
expect(runPushMock).not.toHaveBeenCalled();
});
});
describe('cycle order', () => {
it('runs ensureRepo -> ensureBranch(docmost,main) -> checkout(docmost) -> applyPullActions in order', async () => {
const order: string[] = [];
const built = build({
vaultOverrides: {
ensureRepo: jest.fn(async () => {
order.push('ensureRepo');
}),
ensureBranch: jest.fn(async (branch: string, base: string) => {
order.push(`ensureBranch:${branch}:${base}`);
}),
checkout: jest.fn(async (branch: string) => {
order.push(`checkout:${branch}`);
}),
},
});
applyPullActionsMock.mockImplementation(async () => {
order.push('applyPullActions');
return { written: 0, deleted: 0, merge: { conflict: false } };
});
await built.orchestrator.runOnce('space-1', 'ws-1');
expect(order).toEqual([
'ensureRepo',
'ensureBranch:docmost:main',
'checkout:docmost',
'applyPullActions',
]);
});
});
describe('delete cap (anti-data-loss)', () => {
it('neutralizes deletePage on the apply client when planned deletes exceed the cap', async () => {
jest.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined);
const built = build({ maxDeletes: 5 });
// Dry-run plans 9 deletes (over the cap of 5); apply still runs.
runPushMock
.mockResolvedValueOnce({ mode: 'plan', failures: [], planned: { deletes: 9 } })
.mockResolvedValueOnce({ mode: 'apply', failures: [], planned: { deletes: 0 } });
const res = await built.orchestrator.runOnce('space-1', 'ws-1');
expect(res.ran).toBe(true);
expect(runPushMock).toHaveBeenCalledTimes(2);
// The second runPush (real apply, dryRun:false) got a neutralized client.
const [applyDeps, applyOpts] = runPushMock.mock.calls[1];
expect(applyOpts).toEqual({ dryRun: false });
const applyClient = applyDeps.makeClient();
// deletePage is still a function (the engine may call it)...
expect(typeof applyClient.deletePage).toBe('function');
await applyClient.deletePage('p1');
// ...but it is a NO-OP: the underlying real deletePage was NOT invoked.
expect(built.client.deletePage).not.toHaveBeenCalled();
// Creates/updates pass through to the real client.
expect(applyClient.createPage).toBe(built.client.createPage);
});
it('fails safe: a throwing dry-run still suppresses deletes and does not throw', async () => {
jest.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined);
const built = build({ maxDeletes: 5 });
runPushMock
.mockRejectedValueOnce(new Error('plan failed'))
.mockResolvedValueOnce({ mode: 'apply', failures: [], planned: { deletes: 0 } });
const res = await built.orchestrator.runOnce('space-1', 'ws-1');
// The cycle still completes (ran:true), it does NOT throw.
expect(res.ran).toBe(true);
const [applyDeps] = runPushMock.mock.calls[1];
const applyClient = applyDeps.makeClient();
await applyClient.deletePage('p1');
expect(built.client.deletePage).not.toHaveBeenCalled();
});
it('passes through the original client when planned deletes are within the cap', async () => {
const built = build({ maxDeletes: 5 });
runPushMock
.mockResolvedValueOnce({ mode: 'plan', failures: [], planned: { deletes: 3 } })
.mockResolvedValueOnce({ mode: 'apply', failures: [], planned: { deletes: 0 } });
await built.orchestrator.runOnce('space-1', 'ws-1');
const [applyDeps] = runPushMock.mock.calls[1];
const applyClient = applyDeps.makeClient();
// The ORIGINAL client is used (deletePage forwards to the real one).
expect(applyClient).toBe(built.client);
await applyClient.deletePage('p1');
expect(built.client.deletePage).toHaveBeenCalledWith('p1');
});
});
describe('remote template substitution', () => {
it('substitutes {spaceId} into the gitRemote handed to runPush', async () => {
const built = build({ remoteTemplate: 'git@h:vault-{spaceId}.git' });
await built.orchestrator.runOnce('space-42', 'ws-1');
// Inspect the settings on the dry-run call (first runPush).
const [dryDeps] = runPushMock.mock.calls[0];
expect(dryDeps.settings.gitRemote).toBe('git@h:vault-space-42.git');
});
});
describe('module lifecycle', () => {
it('registers exactly one interval on init and tears it down idempotently on destroy', () => {
const built = build();
jest.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined);
built.orchestrator.onModuleInit();
expect(built.scheduler.addInterval).toHaveBeenCalledTimes(1);
const [name] = built.scheduler.addInterval.mock.calls[0];
built.orchestrator.onModuleDestroy();
expect(built.scheduler.deleteInterval).toHaveBeenCalledTimes(1);
expect(built.scheduler.deleteInterval).toHaveBeenCalledWith(name);
// A second destroy is a no-op (guard against double-delete).
built.orchestrator.onModuleDestroy();
expect(built.scheduler.deleteInterval).toHaveBeenCalledTimes(1);
});
it('registers nothing on init when git-sync is disabled', () => {
const built = build({ enabled: false });
built.orchestrator.onModuleInit();
expect(built.scheduler.addInterval).not.toHaveBeenCalled();
});
});
});

View File

@@ -0,0 +1,67 @@
// Unit tests for the per-space vault path resolver + lazy VaultGit cache
// (plan §3/§5). `mkdir` and `VaultGit` are mocked so construction is cheap and
// no real filesystem / git work happens. We assert the path normalization
// (trailing slash) and the one-VaultGit-per-space caching contract.
import { mkdir } from 'node:fs/promises';
import { VaultGit } from '@docmost/git-sync';
jest.mock('node:fs/promises', () => ({
mkdir: jest.fn(async () => undefined),
}));
// Cheap VaultGit stub: records the path it was constructed with; no shell-out.
jest.mock('@docmost/git-sync', () => ({
VaultGit: jest.fn().mockImplementation((path: string) => ({ path })),
}));
import { VaultRegistryService } from './vault-registry.service';
type AnyMock = jest.Mock;
const mkdirMock = mkdir as unknown as AnyMock;
const VaultGitMock = VaultGit as unknown as AnyMock;
function build(dataDir: string): { service: VaultRegistryService } {
const env = {
getGitSyncDataDir: jest.fn(() => dataDir),
};
const service = new VaultRegistryService(env as any);
return { service };
}
beforeEach(() => {
jest.clearAllMocks();
});
describe('VaultRegistryService', () => {
describe('vaultPath', () => {
it('normalizes a trailing slash in the data dir (no double slash)', () => {
const { service } = build('/vaults/');
expect(service.vaultPath('space-1')).toBe('/vaults/space-1');
});
it('works without a trailing slash too', () => {
const { service } = build('/vaults');
expect(service.vaultPath('space-1')).toBe('/vaults/space-1');
});
});
describe('getVault lazy cache', () => {
it('returns the SAME instance on a second call (one VaultGit per space)', async () => {
const { service } = build('/vaults');
const first = await service.getVault('space-1');
const second = await service.getVault('space-1');
// Same cached instance, constructed exactly once.
expect(second).toBe(first);
expect(VaultGitMock).toHaveBeenCalledTimes(1);
expect(VaultGitMock).toHaveBeenCalledWith('/vaults/space-1');
// mkdir is only run on the first (cache-miss) construction.
expect(mkdirMock).toHaveBeenCalledTimes(1);
expect(mkdirMock).toHaveBeenCalledWith('/vaults/space-1', {
recursive: true,
});
});
});
});