fix(git-sync): subpages round-trips (was {{SUBPAGES}} literal) + exhaustive all-node round-trip test
subpages exported to the literal `{{SUBPAGES}}`, which has no markdown/HTML
inverse, so on re-import it came back as a plain paragraph holding the visible
text "{{SUBPAGES}}" — the embed rendered as that literal string on the page
after a sync (round-trip data loss, seen live). It now emits the schema-matching
`<div data-type="subpages">` like every other embed node, so the schema's
parseHTML rebuilds the subpages node. Also dropped the leaf-atom content-hole
in the subpages renderHTML.
New committed regression coverage:
- packages/git-sync/test/roundtrip-all-nodes.test.ts — exhaustive serialize ->
deserialize round trip for ALL 40 node/mark types; each asserts the node/mark
survives and no `{{...}}` literal leaks. This is the test that caught subpages.
- §13.1 gate (git-sync-converter-gate.spec.ts): subpages added to the green
corpus (round-trips through the REAL server schema).
- Corrected two PR-authored tests that asserted the old {{SUBPAGES}} loss as
"by design" — they now assert the fixed round trip.
Also folds in review #1679 coverage-gap tests (no prod change): orchestrator
pollTick/enabledSpaces, datasource 3-way merge dispatch, page.repo
last_updated_source provenance SQL.
git-sync vitest 659 (+1 expected-fail), server tsc clean, server specs green.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -360,6 +360,15 @@ const CORPUS: Record<string, any> = {
|
||||
content: [para(text('quote at the end'))],
|
||||
},
|
||||
),
|
||||
|
||||
// Atom embeds that carry no inline text: they must round-trip via their
|
||||
// schema-matching HTML (data-type div), NOT a literal that re-imports as plain
|
||||
// text. `subpages` used to export as the literal "{{SUBPAGES}}" and came back
|
||||
// as visible text on the page (red-team round-trip data loss) — this locks it.
|
||||
// editor-ext materializes the `recursive: false` default on import, so the
|
||||
// fixture pre-authors it to sit at the round-trip fixpoint (matches the other
|
||||
// default-materializing fixtures above).
|
||||
'subpages embed': doc({ type: 'subpages', attrs: { recursive: false } }),
|
||||
};
|
||||
|
||||
describe('git-sync converter §13.1 idempotency gate (editor-ext schema)', () => {
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
import {
|
||||
Kysely,
|
||||
CamelCasePlugin,
|
||||
DummyDriver,
|
||||
PostgresAdapter,
|
||||
PostgresIntrospector,
|
||||
PostgresQueryCompiler,
|
||||
CompiledQuery,
|
||||
} from 'kysely';
|
||||
import { PageRepo } from './page.repo';
|
||||
import type { KyselyDB } from '../../types/kysely.types';
|
||||
|
||||
/**
|
||||
* SQL-builder unit test for the git-sync provenance stamp on PageRepo's
|
||||
* soft-delete / restore paths (PR #119 review). Both `removePage` and
|
||||
* `restorePage` take an optional `lastUpdatedSource` arg and conditionally fold
|
||||
* it into the recursive-subtree `UPDATE pages SET ...` via
|
||||
* `...(lastUpdatedSource ? { lastUpdatedSource } : {})`. The change-listener
|
||||
* loop-guard reads `last_updated_source = 'git-sync'` to recognize git-sync's own
|
||||
* writes and skip the echo cycle; this test guards that the stamp is present when
|
||||
* the arg is supplied and ABSENT when it is omitted (an ordinary user delete must
|
||||
* not clobber the column).
|
||||
*
|
||||
* Harness: the same compile-only Kysely/DummyDriver pattern as
|
||||
* space.repo.spec.ts, plus the production `CamelCasePlugin` (so the compiled SQL
|
||||
* carries the real snake_case column names, e.g. `last_updated_source`) and a
|
||||
* thin driver that returns ONE fixed row for every query. The fixed row is what
|
||||
* lets the repo's guard reads (root snapshot / recursive descendants / restore
|
||||
* target) resolve non-empty so execution reaches the subtree UPDATE we assert on
|
||||
* — a bare DummyDriver returns no rows and both methods short-circuit before the
|
||||
* update. We never hit a real database; we capture each compiled statement via
|
||||
* Kysely's `log` hook and inspect the `update "pages" set ...` SQL.
|
||||
*/
|
||||
describe('PageRepo — git-sync provenance on soft-delete / restore SQL', () => {
|
||||
// A single row shaped to satisfy every column the repo reads off its guard
|
||||
// queries. `parentPageId: null` keeps restorePage on the simple path (no
|
||||
// parent-detach UPDATE), so the only `update "pages"` statement is the one we
|
||||
// assert on.
|
||||
const FIXED_ROW = {
|
||||
id: 'p1',
|
||||
slugId: 's1',
|
||||
title: 'Doc',
|
||||
icon: null,
|
||||
position: 'a0',
|
||||
spaceId: 'space-1',
|
||||
parentPageId: null,
|
||||
deletedAt: null,
|
||||
};
|
||||
|
||||
class FixedRowDriver extends DummyDriver {
|
||||
async acquireConnection(): Promise<any> {
|
||||
return {
|
||||
async executeQuery() {
|
||||
return { rows: [{ ...FIXED_ROW }] };
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
async *streamQuery() {},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
interface Captured {
|
||||
sql: string;
|
||||
parameters: readonly unknown[];
|
||||
}
|
||||
|
||||
// Compile-only Kysely on the Postgres dialect (CamelCasePlugin for real column
|
||||
// names) whose `log` hook records every executed statement's compiled SQL.
|
||||
function makeRepoCapturingSql() {
|
||||
const captured: Captured[] = [];
|
||||
const db = new Kysely<any>({
|
||||
dialect: {
|
||||
createAdapter: () => new PostgresAdapter(),
|
||||
createDriver: () => new FixedRowDriver(),
|
||||
createIntrospector: (d) => new PostgresIntrospector(d),
|
||||
createQueryCompiler: () => new PostgresQueryCompiler(),
|
||||
},
|
||||
plugins: [new CamelCasePlugin()],
|
||||
log: (event) => {
|
||||
if (event.level === 'query') {
|
||||
const q = event.query as CompiledQuery;
|
||||
captured.push({ sql: q.sql, parameters: q.parameters });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const repo = new PageRepo(
|
||||
db as unknown as KyselyDB,
|
||||
{} as any,
|
||||
{ emit: jest.fn() } as any,
|
||||
);
|
||||
// Find the single subtree UPDATE on pages (collapse whitespace for matching).
|
||||
const getUpdatePagesSql = (): Captured | undefined =>
|
||||
captured
|
||||
.map((c) => ({ ...c, sql: c.sql.replace(/\s+/g, ' ') }))
|
||||
.find((c) => /update "pages" set/i.test(c.sql));
|
||||
return { repo, getUpdatePagesSql };
|
||||
}
|
||||
|
||||
describe('removePage', () => {
|
||||
it("stamps last_updated_source = 'git-sync' on the subtree soft-delete when the provenance arg is supplied", async () => {
|
||||
const { repo, getUpdatePagesSql } = makeRepoCapturingSql();
|
||||
|
||||
await repo.removePage('p1', 'user-1', 'ws-1', 'git-sync');
|
||||
|
||||
const update = getUpdatePagesSql();
|
||||
expect(update).toBeDefined();
|
||||
// The provenance column is in the UPDATE's SET clause...
|
||||
expect(update!.sql).toContain('"last_updated_source" =');
|
||||
// ...with the 'git-sync' marker as the bound value.
|
||||
expect(update!.parameters).toContain('git-sync');
|
||||
// Sanity: it is still the soft-delete UPDATE (sets deleted_at too).
|
||||
expect(update!.sql).toContain('"deleted_at" =');
|
||||
});
|
||||
|
||||
it('OMITS last_updated_source from the soft-delete when the provenance arg is undefined', async () => {
|
||||
const { repo, getUpdatePagesSql } = makeRepoCapturingSql();
|
||||
|
||||
await repo.removePage('p1', 'user-1', 'ws-1');
|
||||
|
||||
const update = getUpdatePagesSql();
|
||||
expect(update).toBeDefined();
|
||||
// Ordinary user delete: the column must NOT be touched (keeps prior value).
|
||||
expect(update!.sql).not.toContain('last_updated_source');
|
||||
expect(update!.parameters).not.toContain('git-sync');
|
||||
// It is still the soft-delete UPDATE.
|
||||
expect(update!.sql).toContain('"deleted_at" =');
|
||||
});
|
||||
});
|
||||
|
||||
describe('restorePage', () => {
|
||||
it("stamps last_updated_source = 'git-sync' on the subtree restore when the provenance arg is supplied", async () => {
|
||||
const { repo, getUpdatePagesSql } = makeRepoCapturingSql();
|
||||
|
||||
await repo.restorePage('p1', 'ws-1', 'git-sync');
|
||||
|
||||
const update = getUpdatePagesSql();
|
||||
expect(update).toBeDefined();
|
||||
expect(update!.sql).toContain('"last_updated_source" =');
|
||||
expect(update!.parameters).toContain('git-sync');
|
||||
// Sanity: it is the restore UPDATE (clears deleted_at).
|
||||
expect(update!.sql).toContain('"deleted_at" =');
|
||||
});
|
||||
|
||||
it('OMITS last_updated_source from the restore when the provenance arg is undefined', async () => {
|
||||
const { repo, getUpdatePagesSql } = makeRepoCapturingSql();
|
||||
|
||||
await repo.restorePage('p1', 'ws-1');
|
||||
|
||||
const update = getUpdatePagesSql();
|
||||
expect(update).toBeDefined();
|
||||
expect(update!.sql).not.toContain('last_updated_source');
|
||||
expect(update!.parameters).not.toContain('git-sync');
|
||||
expect(update!.sql).toContain('"deleted_at" =');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -19,6 +19,14 @@ jest.mock('../git-sync.loader', () => ({
|
||||
}));
|
||||
|
||||
import { Logger } from '@nestjs/common';
|
||||
import {
|
||||
Kysely,
|
||||
DummyDriver,
|
||||
PostgresAdapter,
|
||||
PostgresIntrospector,
|
||||
PostgresQueryCompiler,
|
||||
CompiledQuery,
|
||||
} from 'kysely';
|
||||
import {
|
||||
GitSyncOrchestrator,
|
||||
GitSyncLockHeldError,
|
||||
@@ -466,4 +474,91 @@ describe('GitSyncOrchestrator', () => {
|
||||
expect(built.scheduler.addInterval).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// The poll-safety backstop: each tick enumerates the STRICT opt-in spaces and
|
||||
// reconciles each one under its own lock. We drive the private `pollTick()`
|
||||
// directly and (separately) compile `enabledSpaces()` to assert its opt-in SQL.
|
||||
describe('pollTick + enabledSpaces (strict opt-in backstop)', () => {
|
||||
it('runs runOnce exactly once per enabled space, with the right (spaceId, workspaceId)', async () => {
|
||||
const built = build();
|
||||
// Isolate the tick wiring from the cycle machinery: stub the enumeration
|
||||
// and count runOnce (it never throws; here we don't exercise its body).
|
||||
const runOnce = jest
|
||||
.spyOn(built.orchestrator, 'runOnce')
|
||||
.mockResolvedValue({ spaceId: 'x', ran: true });
|
||||
jest
|
||||
.spyOn(built.orchestrator as any, 'enabledSpaces')
|
||||
.mockResolvedValue([
|
||||
{ spaceId: 'space-1', workspaceId: 'ws-1' },
|
||||
{ spaceId: 'space-2', workspaceId: 'ws-2' },
|
||||
]);
|
||||
|
||||
await (built.orchestrator as any).pollTick();
|
||||
|
||||
expect(runOnce).toHaveBeenCalledTimes(2);
|
||||
// Per-space isolation: each space is reconciled with its OWN workspace id.
|
||||
expect(runOnce).toHaveBeenNthCalledWith(1, 'space-1', 'ws-1');
|
||||
expect(runOnce).toHaveBeenNthCalledWith(2, 'space-2', 'ws-2');
|
||||
});
|
||||
|
||||
it('does NOT throw and runs nothing when the enabled-spaces query throws (try/catch backstop)', async () => {
|
||||
jest.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);
|
||||
const built = build();
|
||||
const runOnce = jest.spyOn(built.orchestrator, 'runOnce');
|
||||
jest
|
||||
.spyOn(built.orchestrator as any, 'enabledSpaces')
|
||||
.mockRejectedValue(new Error('db down'));
|
||||
|
||||
// A failed enumeration must never break the interval — pollTick swallows it.
|
||||
await expect(
|
||||
(built.orchestrator as any).pollTick(),
|
||||
).resolves.toBeUndefined();
|
||||
expect(runOnce).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('early-returns (no enumeration, no runOnce) when git-sync is disabled', async () => {
|
||||
const built = build({ enabled: false });
|
||||
const enabled = jest.spyOn(built.orchestrator as any, 'enabledSpaces');
|
||||
const runOnce = jest.spyOn(built.orchestrator, 'runOnce');
|
||||
|
||||
await (built.orchestrator as any).pollTick();
|
||||
|
||||
// Gated on the master switch before any DB work.
|
||||
expect(enabled).not.toHaveBeenCalled();
|
||||
expect(runOnce).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('compiles the STRICT opt-in enumeration SQL (spaces, deletedAt is null, enabled flag)', async () => {
|
||||
// Inject a compile-only Kysely (DummyDriver) whose `log` hook captures the
|
||||
// exact SQL `enabledSpaces()` runs — no fake builder, the real query is
|
||||
// compiled. DummyDriver yields no rows; we only assert the SQL shape.
|
||||
const built = build();
|
||||
let captured: CompiledQuery | undefined;
|
||||
const compileDb = new Kysely<any>({
|
||||
dialect: {
|
||||
createAdapter: () => new PostgresAdapter(),
|
||||
createDriver: () => new DummyDriver(),
|
||||
createIntrospector: (d) => new PostgresIntrospector(d),
|
||||
createQueryCompiler: () => new PostgresQueryCompiler(),
|
||||
},
|
||||
log: (event) => {
|
||||
if (event.level === 'query') captured = event.query as CompiledQuery;
|
||||
},
|
||||
});
|
||||
// Swap the orchestrator's injected db for the compile-only instance.
|
||||
(built.orchestrator as any).db = compileDb;
|
||||
|
||||
const rows = await (built.orchestrator as any).enabledSpaces();
|
||||
// DummyDriver returns no rows -> empty opt-in list (the no-space default).
|
||||
expect(rows).toEqual([]);
|
||||
|
||||
expect(captured).toBeDefined();
|
||||
const sql = captured!.sql.replace(/\s+/g, ' ');
|
||||
expect(sql).toContain('from "spaces"');
|
||||
// deletedAt-is-null guard (live spaces only).
|
||||
expect(sql).toContain('"deletedAt" is null');
|
||||
// STRICT per-space opt-in: the raw jsonb flag predicate, verbatim.
|
||||
expect(sql).toContain(`settings->'gitSync'->>'enabled' = 'true'`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -48,6 +48,11 @@ jest.mock('../git-sync.loader', () => ({
|
||||
|
||||
import * as Y from 'yjs';
|
||||
import { GitmostDataSourceService } from './gitmost-datasource.service';
|
||||
// The body-write seam picks 2-way vs 3-way merge based on whether a base doc was
|
||||
// built. We spy on the real module exports (ts-jest CJS output references them
|
||||
// through the namespace object, so the spies intercept the SUT's calls) and let
|
||||
// them call through, so we assert WHICH merge ran without mocking the behaviour.
|
||||
import * as bodyMerge from './yjs-body-merge';
|
||||
|
||||
// Focused unit/contract test for the native GitSyncClient adapter.
|
||||
// No DB, no real collab server: the repos/services/gateway are mocked and we
|
||||
@@ -271,6 +276,46 @@ describe('GitmostDataSourceService', () => {
|
||||
// The body fragment is non-empty: the incoming block was merged in.
|
||||
expect(realDoc.getXmlFragment('default').length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
// The 2-way path (no base) is covered above; this exercises the THREE-WAY
|
||||
// branch that only fires when a `baseMarkdown` is supplied (review #5).
|
||||
describe('with a baseMarkdown (three-way merge)', () => {
|
||||
afterEach(() => jest.restoreAllMocks());
|
||||
|
||||
it('builds a base doc and dispatches to mergeXmlFragments3Way (not the 2-way merge)', async () => {
|
||||
const { service, mocks } = build();
|
||||
mocks.pageRepo.findById.mockResolvedValue({
|
||||
id: 'p1',
|
||||
updatedAt: new Date('2026-06-20T11:00:00.000Z'),
|
||||
});
|
||||
// Spy through to the real implementations so we observe the dispatch.
|
||||
const merge3 = jest.spyOn(bodyMerge, 'mergeXmlFragments3Way');
|
||||
const merge2 = jest.spyOn(bodyMerge, 'mergeXmlFragments');
|
||||
|
||||
await service
|
||||
.bind(CTX)
|
||||
.importPageMarkdown('p1', '# Full\n\ngit', '# Base\n\nbase');
|
||||
|
||||
// The body write was staged through collab as before.
|
||||
expect(mocks.conn.transact).toHaveBeenCalledTimes(1);
|
||||
expect(typeof mocks.conn.capturedFn).toBe('function');
|
||||
|
||||
// Running the captured merge against a real live doc takes the 3-way path:
|
||||
// the base was parsed/built and the 3-way helper is invoked with three
|
||||
// fragments; the 2-way fallback is NOT used.
|
||||
const liveDoc = new Y.Doc();
|
||||
expect(() => mocks.conn.capturedFn?.(liveDoc)).not.toThrow();
|
||||
|
||||
expect(merge3).toHaveBeenCalledTimes(1);
|
||||
expect(merge2).not.toHaveBeenCalled();
|
||||
const [liveFrag, gitFrag, baseFrag] = merge3.mock.calls[0];
|
||||
expect(liveFrag).toBeInstanceOf(Y.XmlFragment);
|
||||
expect(gitFrag).toBeInstanceOf(Y.XmlFragment);
|
||||
// The third arg is the BASE fragment — proof the base markdown was parsed
|
||||
// and converted into its own doc for the common-ancestor comparison.
|
||||
expect(baseFrag).toBeInstanceOf(Y.XmlFragment);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('createPage', () => {
|
||||
|
||||
Reference in New Issue
Block a user