1
import { describe, expect, test } from 'vitest';
2
import type { Collection } from '../types/collection.js';
10
} from '../types/snapshot.js';
11
import { validateApplyDiff } from './validate-diff.js';
13
test('should fail on invalid diff schema', () => {
14
const diff = {} as SnapshotDiffWithHash;
15
const snapshot = {} as SnapshotWithHash;
17
expect(() => validateApplyDiff(diff, snapshot)).toThrowError('"hash" is required');
20
test('should fail on invalid hash', () => {
23
diff: { collections: [{ collection: 'test', diff: [] }], fields: [], relations: [] },
24
} as SnapshotDiffWithHash;
26
const snapshot = { hash: 'xyz' } as SnapshotWithHash;
28
expect(() => validateApplyDiff(diff, snapshot)).toThrowError(
29
"Provided hash does not match the current instance's schema hash",
33
describe('should throw accurate error', () => {
34
const baseDiff = (partialDiff: Partial<SnapshotDiff>): SnapshotDiffWithHash => {
46
const baseSnapshot = (partialSnapshot?: Partial<Snapshot>) => {
49
collections: [] as Collection[],
50
fields: [] as SnapshotField[],
51
relations: [] as SnapshotRelation[],
53
} as SnapshotWithHash;
56
test('creating collection which already exists', () => {
57
const diff = baseDiff({
58
collections: [{ collection: 'test', diff: [{ kind: 'N', rhs: {} as Collection }] }],
61
const snapshot = baseSnapshot({ collections: [{ collection: 'test' } as Collection] });
63
expect(() => validateApplyDiff(diff, snapshot)).toThrowError(
64
'Provided diff is trying to create collection "test" but it already exists',
68
test('deleting collection which does not exist', () => {
69
const diff = baseDiff({
70
collections: [{ collection: 'test', diff: [{ kind: 'D', lhs: {} as Collection }] }],
73
expect(() => validateApplyDiff(diff, baseSnapshot())).toThrowError(
74
'Provided diff is trying to delete collection "test" but it does not exist',
78
test('creating field which already exists', () => {
79
const diff = baseDiff({
80
fields: [{ collection: 'test', field: 'test', diff: [{ kind: 'N', rhs: {} as SnapshotField }] }],
83
const snapshot = baseSnapshot({ fields: [{ collection: 'test', field: 'test' } as SnapshotField] });
85
expect(() => validateApplyDiff(diff, snapshot)).toThrowError(
86
'Provided diff is trying to create field "test.test" but it already exists',
90
test('deleting field which does not exist', () => {
91
const diff = baseDiff({
92
fields: [{ collection: 'test', field: 'test', diff: [{ kind: 'D', lhs: {} as SnapshotField }] }],
95
expect(() => validateApplyDiff(diff, baseSnapshot())).toThrowError(
96
'Provided diff is trying to delete field "test.test" but it does not exist',
100
test('creating relation which already exists', () => {
101
const diff = baseDiff({
106
related_collection: 'relation',
107
diff: [{ kind: 'N', rhs: {} as SnapshotRelation }],
112
const snapshot = baseSnapshot({
113
relations: [{ collection: 'test', field: 'test', related_collection: 'relation' } as SnapshotRelation],
116
expect(() => validateApplyDiff(diff, snapshot)).toThrowError(
117
'Provided diff is trying to create relation "test.test-> relation" but it already exists',
121
test('deleting relation which does not exist', () => {
122
const diff = baseDiff({
127
related_collection: 'relation',
128
diff: [{ kind: 'D', lhs: {} as SnapshotRelation }],
133
expect(() => validateApplyDiff(diff, baseSnapshot())).toThrowError(
134
'Provided diff is trying to delete relation "test.test-> relation" but it does not exist',
139
test('should not throw error for diffs with varying types of lhs/rhs', () => {
149
path: ['meta', 'color'],
160
path: ['meta', 'translations'],
166
translation: 'Collection A de-DE',
177
path: ['meta', 'translations', 1, 'language'],
183
path: ['meta', 'translations', 1, 'translation'],
209
field: 'update_field',
213
path: ['meta', 'options'],
215
iconLeft: 'check_circle',
223
field: 'delete_field',
229
field: 'delete_field',
242
related_collection: 'b',
246
path: ['schema', 'on_delete'],
256
const snapshot = { hash: 'abc' } as SnapshotWithHash;
258
expect(() => validateApplyDiff(diff, snapshot)).not.toThrow();
261
test('should not throw error for relation diff with null related_collection (applicable for M2A junction tables)', () => {
269
collection: 'pages_blocks',
271
related_collection: null,
276
collection: 'pages_blocks',
278
related_collection: null,
280
junction_field: 'pages_id',
281
many_collection: 'pages_blocks',
283
one_allowed_collections: ['a', 'b'],
284
one_collection: null,
285
one_collection_field: 'collection',
286
one_deselect_action: 'nullify',
298
const snapshot = { hash: 'abc' } as SnapshotWithHash;
300
expect(() => validateApplyDiff(diff, snapshot)).not.toThrow();
303
test('should detect empty diff', () => {
306
diff: { collections: [], fields: [], relations: [] },
309
const snapshot = {} as SnapshotWithHash;
311
expect(validateApplyDiff(diff, snapshot)).toBe(false);
314
test('should pass on valid diff', () => {
317
diff: { collections: [{ collection: 'test', diff: [] }], fields: [], relations: [] },
320
const snapshot = { hash: 'abc' } as SnapshotWithHash;
322
expect(validateApplyDiff(diff, snapshot)).toBe(true);