Add automatic project snapshots

This commit is contained in:
2026-07-25 23:27:51 +02:00
parent 59d442593f
commit 1a77eedaa8
23 changed files with 2232 additions and 26 deletions
+115
View File
@@ -11,8 +11,10 @@ import {
ProjectRevisionConflictError,
ProjectRevisionRepository,
} from "../src/db/repositories/project-revision.repository.js";
import { ProjectSnapshotRepository } from "../src/db/repositories/project-snapshot.repository.js";
import { projectChangeSets } from "../src/db/schema/project-change-sets.js";
import { projectRevisions } from "../src/db/schema/project-revisions.js";
import { projectSnapshots } from "../src/db/schema/project-snapshots.js";
import { projects } from "../src/db/schema/projects.js";
function createTestDatabase(): DatabaseContext {
@@ -157,4 +159,117 @@ describe("project revision repository", () => {
context.close();
}
});
it("captures and retains automatic snapshots without deleting named snapshots", () => {
const context = createTestDatabase();
try {
const revisions = new ProjectRevisionRepository(context.db);
const snapshots = new ProjectSnapshotRepository(context.db);
const named = snapshots.createNamed({
projectId: "project-1",
expectedRevision: 0,
name: "Automatisch · Revision 25",
});
assert.ok(named);
for (
let expectedRevision = 0;
expectedRevision < 325;
expectedRevision += 1
) {
appendTestRevision(revisions, expectedRevision);
}
const stored = context.db
.select({
id: projectSnapshots.id,
kind: projectSnapshots.kind,
name: projectSnapshots.name,
sourceRevision: projectSnapshots.sourceRevision,
createdByActorId: projectSnapshots.createdByActorId,
})
.from(projectSnapshots)
.all();
const automatic = stored
.filter((snapshot) => snapshot.kind === "automatic")
.sort(
(left, right) =>
left.sourceRevision - right.sourceRevision
);
assert.equal(automatic.length, 12);
assert.deepEqual(
automatic.map((snapshot) => snapshot.sourceRevision),
[50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325]
);
assert.equal(
automatic.every(
(snapshot) => snapshot.createdByActorId === "test-user"
),
true
);
assert.equal(
automatic.some(
(snapshot) =>
snapshot.name === "Automatisch · Revision 25"
),
false
);
assert.deepEqual(
stored.filter((snapshot) => snapshot.kind === "named"),
[
{
id: named.id,
kind: "named",
name: "Automatisch · Revision 25",
sourceRevision: 0,
createdByActorId: null,
},
]
);
} finally {
context.close();
}
});
it("rolls back revision 25 when automatic snapshot persistence fails", () => {
const context = createTestDatabase();
try {
const repository = new ProjectRevisionRepository(context.db);
for (
let expectedRevision = 0;
expectedRevision < 24;
expectedRevision += 1
) {
appendTestRevision(repository, expectedRevision);
}
context.sqlite.exec(`
CREATE TRIGGER fail_automatic_project_snapshot
BEFORE INSERT ON project_snapshots
WHEN NEW.kind = 'automatic'
BEGIN
SELECT RAISE(ABORT, 'forced automatic snapshot failure');
END;
`);
assert.throws(
() => appendTestRevision(repository, 24),
/forced automatic snapshot failure/
);
assert.equal(repository.getCurrentRevision("project-1"), 24);
assert.equal(
context.db.select().from(projectRevisions).all().length,
24
);
assert.equal(
context.db.select().from(projectChangeSets).all().length,
24
);
assert.equal(
context.db.select().from(projectSnapshots).all().length,
0
);
} finally {
context.close();
}
});
});
+41
View File
@@ -0,0 +1,41 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
automaticProjectSnapshotIntervalRevisions,
automaticProjectSnapshotRetentionCount,
shouldCaptureAutomaticProjectSnapshot,
} from "../src/domain/models/project-snapshot-policy.model.js";
describe("automatic project snapshot policy", () => {
it("captures every 25 revisions and retains twelve automatic snapshots", () => {
assert.equal(automaticProjectSnapshotIntervalRevisions, 25);
assert.equal(automaticProjectSnapshotRetentionCount, 12);
assert.equal(
shouldCaptureAutomaticProjectSnapshot(24, null),
false
);
assert.equal(
shouldCaptureAutomaticProjectSnapshot(25, null),
true
);
assert.equal(
shouldCaptureAutomaticProjectSnapshot(49, 25),
false
);
assert.equal(
shouldCaptureAutomaticProjectSnapshot(50, 25),
true
);
});
it("rejects invalid or inconsistent revision inputs", () => {
assert.throws(
() => shouldCaptureAutomaticProjectSnapshot(-1, null),
/non-negative integer/
);
assert.throws(
() => shouldCaptureAutomaticProjectSnapshot(25, 26),
/exceeds current revision/
);
});
});
+6
View File
@@ -16,6 +16,7 @@ import {
import {
getProjectRevisionDescription,
getProjectRevisionSourceLabel,
getProjectSnapshotKindLabel,
mergeProjectRevisionPages,
} from "../src/frontend/utils/project-version-history.js";
@@ -79,6 +80,11 @@ describe("project version history presentation", () => {
),
"future.command"
);
assert.equal(getProjectSnapshotKindLabel("named"), "Benannt");
assert.equal(
getProjectSnapshotKindLabel("automatic"),
"Automatisch"
);
});
it("merges paginated revisions without duplicates in descending order", () => {