Files
docmost-sync/test/run-push-realgit.test.ts
vvzvlad 5826255a2f feat(sync): runnable FS->Docmost push (dry-run default, --apply writes)
Wire the push cycle (SPEC §6) into a runnable command; SAFE BY DEFAULT.

- runPush + main(): dry-run by default (plan only, ZERO Docmost writes, no ref
  advance); --apply is the ONLY path that builds a client and mutates Docmost
- orchestration mirrors pull.ts: assertGitAvailable -> ensureRepo ->
  merge-in-progress guard (§9/§12) -> checkout main -> commit local working tree
  (Docmost-Sync-Source: local, §7.3) -> base = refs/docmost/last-pushed else
  docmost -> diffNameStatus(base, main) -> computePushActions -> (apply) ->
  write-back created pageIds + advance refs; divergent-docmost escalates (exit 1)
- npm run push (dry-run) / npm run push -- --apply (writes; needs creds)
- fix (review Blocker): pass the WHOLE VaultGit to applyPushActions (bare method
  refs lost `this` -> --apply crashed on real git); regression test exercises the
  --apply path against a REAL VaultGit temp repo + fake client (proven to catch it)
- symmetric divergent-docmost escalation in both ff branches; dry-run logs the
  local commit explicitly; SPEC §6 notes the dry-run/local-commit behavior
- 737 -> 747 green (x2 stable); build clean; corpus STABLE

Deferred (daemon increment): FS-watcher/debounce (§7.1), git-remote push (§7.2),
continuous poll loop, pull-side §10 record consumption, fractional-index position.
2026-06-21 02:30:46 +03:00

143 lines
5.4 KiB
TypeScript

import { execFile } from 'node:child_process';
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { promisify } from 'node:util';
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
import { runPush, LAST_PUSHED_REF } from '../src/push.js';
import type { PushDeps } from '../src/push.js';
import { VaultGit } from '../src/git.js';
import type { Settings } from '../src/settings.js';
import { serializeDocmostMarkdownBody } from '../packages/docmost-client/src/lib/markdown-document.js';
const execFileAsync = promisify(execFile);
// runPush `--apply` against a REAL VaultGit in a temp repo (NO Docmost — the
// client is faked). This guards the real-git BINDING contract that the plain-
// object git fakes in run-push.test.ts cannot catch: the applier's git deps
// (`updateRef`/`fastForwardBranch`/`showFileAtRef`) call `this.run`/`this.runRaw`
// internally, so they only work when their `this` receiver is preserved. Passing
// bare method references (`git.updateRef`, …) would throw `this.runRaw is not a
// function` here. Only the LOCAL temp git is mutated; nothing is sent to Docmost.
/** True if a usable `git` binary is on PATH (skip the suite otherwise). */
async function gitAvailable(): Promise<boolean> {
try {
await execFileAsync('git', ['--version']);
return true;
} catch {
return false;
}
}
/** A minimal valid Settings fixture (only fields runPush reads matter). */
function makeSettings(vaultPath: string): Settings {
return {
docmostApiUrl: 'https://docmost.example.com',
docmostEmail: 'you@example.com',
docmostPassword: 'secret',
docmostSpaceId: 'space-1',
vaultPath,
pollIntervalMs: 15000,
debounceMs: 2000,
logLevel: 'info',
};
}
/** A recording client fake; createPage returns an assigned id + updatedAt. */
function makeClientFake() {
return {
importPageMarkdown: vi.fn(async () => ({
data: { updatedAt: '2026-06-20T00:00:00.000Z' },
success: true,
})),
createPage: vi.fn(async (title: string) => ({
data: { id: 'new-id', title, updatedAt: '2026-06-20T00:00:00.000Z' },
success: true,
})),
deletePage: vi.fn(async () => ({ success: true })),
movePage: vi.fn(async () => ({ success: true })),
renamePage: vi.fn(async () => ({ success: true })),
};
}
describe('runPush --apply against a REAL VaultGit (binding contract)', () => {
let available = false;
let dir: string;
beforeAll(async () => {
available = await gitAvailable();
});
afterEach(async () => {
if (dir) {
await rm(dir, { recursive: true, force: true });
}
});
it('writes through real git: createPage runs, last-pushed advances, no throw', async () => {
if (!available) return; // skip gracefully when git is unavailable
// Temp vault repo under the OS tmpdir (mirrors test/git.test.ts setup).
dir = await mkdtemp(join(tmpdir(), 'docmost-push-realgit-'));
const vault = dir;
const git = new VaultGit(vault);
await git.ensureRepo();
// The `docmost` mirror branches off `main` at the initial commit; this is
// also the diff base (last-pushed is unset, so runPush falls back to it).
await git.ensureBranch('docmost', 'main');
// A brand-new local file with meta carrying title + spaceId but NO pageId,
// committed on `main` AHEAD of the base -> computePushActions yields a CREATE.
const newFile = serializeDocmostMarkdownBody(
{ version: 1, title: 'New', spaceId: 'sp-1' },
'fresh body',
);
await writeFile(join(vault, 'New.md'), newFile, 'utf8');
await git.stageAll();
await git.commit('add New.md', {
authorName: 'Human',
authorEmail: 'human@local',
});
// last-pushed must be UNSET so the run actually advances it for the first time.
expect(await git.revParse(LAST_PUSHED_REF)).toBeNull();
const client = makeClientFake();
const logs: string[] = [];
const deps: PushDeps = {
settings: makeSettings(vault),
// The WHOLE real VaultGit — its methods must keep their `this` binding.
git,
makeClient: () => client as any,
readFile: (path) =>
import('node:fs/promises').then((fs) =>
fs.readFile(join(vault, ...path.split('/')), 'utf8'),
),
writeFile: async (path, text) => {
const fs = await import('node:fs/promises');
await fs.writeFile(join(vault, ...path.split('/')), text, 'utf8');
},
log: (line) => logs.push(line),
};
// The run must NOT throw — this is what FAILS before Fix 1 (the bare-method
// git deps would throw `this.runRaw is not a function` on the real VaultGit).
const res = await runPush(deps, { dryRun: false });
expect(res.mode).toBe('apply');
expect(res.failures).toEqual([]);
// The FAKE client was actually called (the write path ran).
expect(client.createPage).toHaveBeenCalledTimes(1);
expect(res.applied?.created).toBe(1);
// The assigned pageId was written back to disk + committed.
expect(res.applied?.writtenBack).toEqual([{ path: 'New.md', pageId: 'new-id' }]);
// CRITICALLY: refs/docmost/last-pushed ACTUALLY advanced in the real repo —
// it now resolves to a real commit (proving updateRef ran with binding).
const lastPushed = await git.revParse(LAST_PUSHED_REF);
expect(lastPushed).toMatch(/^[0-9a-f]{40}$/);
expect(res.divergentDocmost).toBe(false);
});
});