Add automatic project snapshots
This commit is contained in:
@@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user