2026-08-20 11:12:32 +07:00
|
|
|
import { test, expect } from "bun:test";
|
|
|
|
|
import { shouldCreateRevision } from "../src/index.service";
|
|
|
|
|
|
|
|
|
|
/**
|
2026-08-21 11:35:27 +07:00
|
|
|
* Unit tests for the revision-dedup decision rule.
|
2026-08-20 11:12:32 +07:00
|
|
|
*
|
|
|
|
|
* `shouldCreateRevision` is the pure predicate that `indexContentFile` consults
|
2026-08-21 11:35:27 +07:00
|
|
|
* before writing a new row to `document_revisions`. It is extracted because the
|
2026-08-20 11:12:32 +07:00
|
|
|
* dedup correctness is the single most important guarantee of the revision
|
|
|
|
|
* system ("metadata-only edits don't bloat history"), and it must hold without
|
|
|
|
|
* a database.
|
|
|
|
|
*
|
|
|
|
|
* The DB-backed paths (`snapshotRevision`, `restoreRevision`) are exercised
|
2026-08-21 11:35:27 +07:00
|
|
|
* end-to-end via the indexer and the web revision restore route. Here we
|
2026-08-20 11:12:32 +07:00
|
|
|
* lock the decision invariant in CI.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
test("shouldCreateRevision: first snapshot when no prior revision exists", () => {
|
|
|
|
|
// No prior revision row → always snapshot the first version.
|
|
|
|
|
expect(shouldCreateRevision(null, "body text")).toBe(true);
|
|
|
|
|
expect(shouldCreateRevision(undefined, "body text")).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("shouldCreateRevision: identical body creates no new revision (dedup)", () => {
|
|
|
|
|
// The exact dedup rule that prevents metadata-only edits from bloating
|
|
|
|
|
// history: if the body is byte-identical to the latest revision's body,
|
|
|
|
|
// skip the snapshot.
|
|
|
|
|
expect(shouldCreateRevision("same body", "same body")).toBe(false);
|
|
|
|
|
// The dedup rule applies regardless of body length — large identical bodies
|
|
|
|
|
// also skip the snapshot.
|
|
|
|
|
expect(shouldCreateRevision("a".repeat(5000), "a".repeat(5000))).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("shouldCreateRevision: changed body creates a new revision", () => {
|
|
|
|
|
expect(shouldCreateRevision("old body", "new body")).toBe(true);
|
|
|
|
|
// Whitespace / trailing newline changes count as a real body change.
|
|
|
|
|
expect(shouldCreateRevision("body", "body\n")).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("shouldCreateRevision: empty-string vs non-empty counts as a change", () => {
|
|
|
|
|
expect(shouldCreateRevision("", "content")).toBe(true);
|
|
|
|
|
expect(shouldCreateRevision("content", "")).toBe(true);
|
|
|
|
|
});
|