-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprotocol-commit-history.test.ts
More file actions
199 lines (178 loc) · 8.62 KB
/
Copy pathprotocol-commit-history.test.ts
File metadata and controls
199 lines (178 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, vi } from 'vitest';
import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol';
/**
* ADR-0067 — package-scoped commit history & rollback.
*
* These tests exercise the commit primitives in isolation with a tiny in-memory
* `sys_metadata_commit` engine fake + a stubbed overlay repo, mirroring the
* `makeProtocol` pattern in protocol-publish-package-drafts.test.ts. They prove
* the revert PLAN semantics (created → soft-remove; edited → restoreVersion) and
* the append-only "a revert is itself a commit" rule, without a real database.
*/
function makeFakeEngine(seedCommits: any[] = []) {
const commits: any[] = [...seedCommits];
const engine: any = {
insert: vi.fn(async (table: string, data: any) => {
if (table === 'sys_metadata_commit') commits.push(data);
}),
find: vi.fn(async (table: string, q: any) => {
if (table === 'sys_metadata_commit') {
return commits.filter((c) => c.package_id === q.where.package_id);
}
return [];
}),
findOne: vi.fn(async (table: string, q: any) => {
if (table === 'sys_metadata_commit') return commits.find((c) => c.id === q.where.id) ?? null;
return null; // no active sys_metadata rows by default
}),
};
return { engine, commits };
}
function makeProtocol(engine: any, repo: any) {
const protocol = new ObjectStackProtocolImplementation(engine as never);
(protocol as any).ensureOverlayIndex = async () => {};
(protocol as any).getOverlayRepo = () => repo;
return protocol;
}
const applyCommit = (over: Partial<any> & { id: string; items: any[]; created_at: string }) => ({
package_id: 'app.edu',
operation: 'apply',
message: 'build',
organization_id: null,
item_count: over.items.length,
...over,
items: JSON.stringify(over.items),
});
describe('ADR-0067 — listCommits', () => {
it('returns [] for a package with no commits', async () => {
const { engine } = makeFakeEngine();
const p = makeProtocol(engine, {});
expect(await p.listCommits({ packageId: 'app.none' })).toEqual([]);
});
it('returns a package’s commits newest-first with parsed items', async () => {
const { engine } = makeFakeEngine([
applyCommit({ id: 'c1', items: [{ type: 'object', name: 'a', existedBefore: false, prevVersion: null }], created_at: '2026-06-24T00:00:01.000Z' }),
applyCommit({ id: 'c2', items: [{ type: 'view', name: 'b', existedBefore: false, prevVersion: null }], created_at: '2026-06-24T00:00:02.000Z' }),
]);
const p = makeProtocol(engine, {});
const list = await p.listCommits({ packageId: 'app.edu' });
expect(list.map((c) => c.id)).toEqual(['c2', 'c1']); // newest-first
expect(list[0].items[0]).toMatchObject({ type: 'view', name: 'b' });
});
});
describe('ADR-0067 — revertCommit', () => {
it('soft-removes artifacts the commit CREATED and records a revert commit', async () => {
const { engine, commits } = makeFakeEngine([
applyCommit({ id: 'cmt_1', items: [{ type: 'object', name: 'course', existedBefore: false, prevVersion: null }], created_at: '2026-06-24T00:00:00.000Z' }),
]);
const del = vi.fn(async () => {});
const repo = { get: vi.fn(async () => ({ hash: 'h1' })), delete: del, restoreVersion: vi.fn() };
const p = makeProtocol(engine, repo);
const res = await p.revertCommit({ commitId: 'cmt_1' });
expect(del).toHaveBeenCalledTimes(1);
expect(res.revertedCount).toBe(1);
expect(res.reverted[0]).toMatchObject({ type: 'object', name: 'course', action: 'removed' });
const revertRow = commits.find((c) => c.operation === 'revert');
expect(revertRow).toBeTruthy();
expect(revertRow.parent_commit_id).toBe('cmt_1');
});
it('restores artifacts the commit EDITED to their prevVersion', async () => {
const { engine } = makeFakeEngine([
applyCommit({ id: 'cmt_2', items: [{ type: 'object', name: 'course', existedBefore: true, prevVersion: 3 }], created_at: '2026-06-24T00:00:00.000Z' }),
]);
const restoreVersion = vi.fn(async () => ({}));
const repo = { get: vi.fn(async () => ({ hash: 'h2' })), delete: vi.fn(), restoreVersion };
const p = makeProtocol(engine, repo);
const res = await p.revertCommit({ commitId: 'cmt_2' });
expect(restoreVersion).toHaveBeenCalledWith(
expect.anything(),
3,
expect.objectContaining({ source: 'protocol.revertCommit' }),
);
expect(res.reverted[0]).toMatchObject({ type: 'object', name: 'course', action: 'restored' });
});
it('throws commit_not_found (404) for an unknown commit', async () => {
const { engine } = makeFakeEngine();
const p = makeProtocol(engine, {});
await expect(p.revertCommit({ commitId: 'nope' })).rejects.toMatchObject({ code: 'commit_not_found', status: 404 });
});
it('reverts dependent artifacts in REVERSE apply order (view before its object)', async () => {
const { engine } = makeFakeEngine([
applyCommit({
id: 'cmt_3',
items: [
{ type: 'object', name: 'course', existedBefore: false, prevVersion: null },
{ type: 'view', name: 'course_list', existedBefore: false, prevVersion: null },
],
created_at: '2026-06-24T00:00:00.000Z',
}),
]);
const order: string[] = [];
const repo = {
get: vi.fn(async () => ({ hash: 'h' })),
delete: vi.fn(async (ref: any) => { order.push(ref.name); }),
restoreVersion: vi.fn(),
};
const p = makeProtocol(engine, repo);
await p.revertCommit({ commitId: 'cmt_3' });
expect(order).toEqual(['course_list', 'course']); // dependents first
});
});
describe('ADR-0067 — rollbackToPackageCommit', () => {
it('reverts every apply commit strictly newer than the target', async () => {
const { engine } = makeFakeEngine([
applyCommit({ id: 'c1', items: [], created_at: '2026-06-24T00:00:01.000Z' }),
applyCommit({ id: 'c2', items: [{ type: 'object', name: 'x', existedBefore: false, prevVersion: null }], created_at: '2026-06-24T00:00:02.000Z' }),
applyCommit({ id: 'c3', items: [{ type: 'view', name: 'y', existedBefore: false, prevVersion: null }], created_at: '2026-06-24T00:00:03.000Z' }),
]);
const repo = { get: vi.fn(async () => ({ hash: 'h' })), delete: vi.fn(async () => {}), restoreVersion: vi.fn() };
const p = makeProtocol(engine, repo);
const res = await p.rollbackToPackageCommit({ commitId: 'c1' });
expect(res.success).toBe(true);
expect([...res.revertedCommits].sort()).toEqual(['c2', 'c3']); // c1 itself kept
});
it('throws commit_not_found for an unknown target', async () => {
const { engine } = makeFakeEngine();
const p = makeProtocol(engine, {});
await expect(p.rollbackToPackageCommit({ commitId: 'ghost' })).rejects.toMatchObject({ code: 'commit_not_found' });
});
});
describe('ADR-0067 — publishPackageDrafts records a commit', () => {
it('records an apply commit carrying the message + aiModel + revert plan', async () => {
const commits: any[] = [];
const engine: any = {
insert: vi.fn(async (t: string, d: any) => { if (t === 'sys_metadata_commit') commits.push(d); }),
findOne: vi.fn(async () => null), // no active rows → every draft is a CREATE
find: vi.fn(async () => []),
};
const protocol = new ObjectStackProtocolImplementation(engine as never);
(protocol as any).ensureOverlayIndex = async () => {};
(protocol as any).getOverlayRepo = () => ({
listDrafts: async () => [{ type: 'object', name: 'course' }],
get: async () => null,
});
// ADR-0067 D2 — the batch promotes via the phase-1 seam (inside one
// transaction), not per-item publishMetaItem; side effects are phase 2.
vi.spyOn(protocol as any, 'promoteDraftForPublish').mockImplementation(async (req: any) => ({
singularType: req.type,
orgId: null,
result: { version: 'h', seq: 7, item: { body: { name: req.name } }, packageId: null },
}));
vi.spyOn(protocol as any, 'runPublishSideEffects').mockResolvedValue({});
const res: any = await (protocol as any).publishPackageDrafts({
packageId: 'app.edu',
message: 'build an education app',
aiModel: 'claude-opus-4-8',
actor: 'ai:claude',
});
expect(res.commitId).toBeTruthy();
const apply = commits.find((c) => c.operation === 'apply');
expect(apply).toBeTruthy();
expect(apply.message).toBe('build an education app');
expect(apply.ai_model).toBe('claude-opus-4-8');
expect(apply.item_count).toBe(1);
const items = JSON.parse(apply.items);
expect(items[0]).toMatchObject({ type: 'object', name: 'course', existedBefore: false });
});
});