@rangka/core 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -2
- package/.claude/skills/extend-core/SKILL.md +0 -133
- package/.turbo/turbo-build.log +0 -4
- package/CHANGELOG.md +0 -25
- package/CLAUDE.md +0 -180
- package/src/__tests__/coerce.test.ts +0 -154
- package/src/__tests__/context.test.ts +0 -111
- package/src/__tests__/helpers.ts +0 -21
- package/src/__tests__/index.test.ts +0 -7
- package/src/__tests__/widgets.test.ts +0 -197
- package/src/api/__tests__/handlers.test.ts +0 -389
- package/src/api/__tests__/include-resolver.test.ts +0 -393
- package/src/api/__tests__/middleware.test.ts +0 -100
- package/src/api/__tests__/openapi-schema.test.ts +0 -210
- package/src/api/__tests__/query-parser.test.ts +0 -291
- package/src/api/__tests__/route-generator.test.ts +0 -137
- package/src/api/__tests__/server.test.ts +0 -73
- package/src/api/__tests__/swagger.test.ts +0 -166
- package/src/api/handlers.ts +0 -274
- package/src/api/include-resolver.ts +0 -27
- package/src/api/index.ts +0 -4
- package/src/api/meta-handler.ts +0 -254
- package/src/api/openapi-schema.ts +0 -99
- package/src/api/query-parser.ts +0 -315
- package/src/api/route-generator.ts +0 -448
- package/src/api/server.ts +0 -147
- package/src/api/types.ts +0 -16
- package/src/audit/__tests__/audit.test.ts +0 -144
- package/src/audit/index.ts +0 -3
- package/src/audit/record.ts +0 -69
- package/src/audit/tables.ts +0 -48
- package/src/audit/types.ts +0 -26
- package/src/auth/__tests__/core-module.test.ts +0 -54
- package/src/auth/__tests__/debug.test.ts +0 -47
- package/src/auth/__tests__/field-permissions.test.ts +0 -245
- package/src/auth/__tests__/integration.test.ts +0 -208
- package/src/auth/__tests__/meta-boot.test.ts +0 -538
- package/src/auth/__tests__/model-permissions.test.ts +0 -205
- package/src/auth/__tests__/password.test.ts +0 -29
- package/src/auth/__tests__/permission-registry.test.ts +0 -313
- package/src/auth/__tests__/scope-hook.test.ts +0 -509
- package/src/auth/__tests__/scope-registry.test.ts +0 -297
- package/src/auth/__tests__/scopes.test.ts +0 -66
- package/src/auth/__tests__/session.test.ts +0 -214
- package/src/auth/core-models.ts +0 -52
- package/src/auth/core-module.ts +0 -59
- package/src/auth/debug.ts +0 -157
- package/src/auth/field-permissions.ts +0 -116
- package/src/auth/index.ts +0 -37
- package/src/auth/model-permissions.ts +0 -59
- package/src/auth/password.ts +0 -22
- package/src/auth/permission-registry.ts +0 -171
- package/src/auth/scope-filters.ts +0 -11
- package/src/auth/scope-registry.ts +0 -121
- package/src/auth/scopes.ts +0 -146
- package/src/auth/seed.ts +0 -44
- package/src/auth/session.ts +0 -178
- package/src/auth/types.ts +0 -50
- package/src/boot/__tests__/page-scanning.test.ts +0 -170
- package/src/boot/__tests__/page-utils.test.ts +0 -225
- package/src/boot/__tests__/project-scanner.test.ts +0 -88
- package/src/boot/dependency-sort.ts +0 -82
- package/src/boot/discovery.ts +0 -85
- package/src/boot/index.ts +0 -457
- package/src/boot/page-utils.ts +0 -110
- package/src/boot/project-scanner.ts +0 -397
- package/src/boot/schema-loader.ts +0 -26
- package/src/boot/schema-merger.ts +0 -125
- package/src/boot/traits.ts +0 -25
- package/src/boot/types.ts +0 -73
- package/src/context.ts +0 -105
- package/src/db/__tests__/cascade-delete.test.ts +0 -182
- package/src/db/__tests__/desired-state.test.ts +0 -136
- package/src/db/__tests__/diff-engine.test.ts +0 -635
- package/src/db/__tests__/field-mapper.test.ts +0 -355
- package/src/db/__tests__/introspect.test.ts +0 -70
- package/src/db/__tests__/search-filter.test.ts +0 -45
- package/src/db/__tests__/sequence.test.ts +0 -221
- package/src/db/auto-sync.ts +0 -133
- package/src/db/client.ts +0 -147
- package/src/db/desired-state.ts +0 -98
- package/src/db/diff-engine.ts +0 -305
- package/src/db/field-mapper.ts +0 -504
- package/src/db/filter-applier.ts +0 -89
- package/src/db/include-resolver.ts +0 -40
- package/src/db/index.ts +0 -23
- package/src/db/introspect.ts +0 -265
- package/src/db/model-include-resolver.ts +0 -327
- package/src/db/model-ops.ts +0 -281
- package/src/db/scope-enforcer.ts +0 -37
- package/src/db/types.ts +0 -98
- package/src/errors.ts +0 -41
- package/src/events/__tests__/bus.test.ts +0 -105
- package/src/events/bus.ts +0 -89
- package/src/events/index.ts +0 -2
- package/src/events/types.ts +0 -9
- package/src/external-model/__tests__/computed-fields.test.ts +0 -106
- package/src/external-model/__tests__/field-mapper.test.ts +0 -160
- package/src/external-model/__tests__/in-memory-ops.test.ts +0 -247
- package/src/external-model/__tests__/mutation-executor.test.ts +0 -160
- package/src/external-model/__tests__/query-executor.test.ts +0 -284
- package/src/external-model/__tests__/schema-converter.test.ts +0 -174
- package/src/external-model/computed-fields.ts +0 -15
- package/src/external-model/define.ts +0 -5
- package/src/external-model/external-model-ops.ts +0 -108
- package/src/external-model/field-mapper.ts +0 -66
- package/src/external-model/in-memory-ops.ts +0 -107
- package/src/external-model/index.ts +0 -7
- package/src/external-model/mutation-executor.ts +0 -71
- package/src/external-model/query-executor.ts +0 -100
- package/src/external-model/schema-converter.ts +0 -53
- package/src/external-model/types.ts +0 -32
- package/src/fixtures/__tests__/fixtures.test.ts +0 -203
- package/src/fixtures/index.ts +0 -10
- package/src/fixtures/loader.ts +0 -196
- package/src/fixtures/registry.ts +0 -125
- package/src/fixtures/types.ts +0 -33
- package/src/helpers/assert-ownership.ts +0 -19
- package/src/helpers/coerce.ts +0 -28
- package/src/helpers/stamping.ts +0 -28
- package/src/helpers/validation.ts +0 -14
- package/src/hooks/__tests__/context.test.ts +0 -73
- package/src/hooks/__tests__/executor.test.ts +0 -433
- package/src/hooks/__tests__/middleware.test.ts +0 -224
- package/src/hooks/__tests__/registry.test.ts +0 -50
- package/src/hooks/context.ts +0 -89
- package/src/hooks/errors.ts +0 -11
- package/src/hooks/executor.ts +0 -115
- package/src/hooks/index.ts +0 -10
- package/src/hooks/middleware.ts +0 -220
- package/src/hooks/registry.ts +0 -20
- package/src/hooks/types.ts +0 -32
- package/src/index.ts +0 -172
- package/src/jobs/__tests__/enqueue.test.ts +0 -77
- package/src/jobs/__tests__/integration.test.ts +0 -71
- package/src/jobs/__tests__/registry.test.ts +0 -103
- package/src/jobs/__tests__/scheduler.test.ts +0 -92
- package/src/jobs/__tests__/worker-execution.test.ts +0 -202
- package/src/jobs/__tests__/worker.test.ts +0 -119
- package/src/jobs/enqueue.ts +0 -93
- package/src/jobs/index.ts +0 -14
- package/src/jobs/registry.ts +0 -92
- package/src/jobs/scheduler.ts +0 -205
- package/src/jobs/tables.ts +0 -132
- package/src/jobs/types.ts +0 -62
- package/src/jobs/worker.ts +0 -272
- package/src/model-api/__tests__/cross-boundary-includes.test.ts +0 -366
- package/src/model-api/__tests__/extended-api.test.ts +0 -244
- package/src/model-api/__tests__/filter-applier.test.ts +0 -177
- package/src/model-api/__tests__/filter-translator.test.ts +0 -186
- package/src/model-api/__tests__/include-resolver.test.ts +0 -226
- package/src/model-api/__tests__/model-access.test.ts +0 -284
- package/src/model-api/__tests__/query-builder.test.ts +0 -224
- package/src/model-api/__tests__/scope-enforcer.test.ts +0 -268
- package/src/model-api/field-access.ts +0 -28
- package/src/model-api/filter-applier.ts +0 -1
- package/src/model-api/filter-translator.ts +0 -67
- package/src/model-api/include-resolver.ts +0 -2
- package/src/model-api/index.ts +0 -86
- package/src/model-api/query-builder.ts +0 -155
- package/src/model-api/scope-enforcer.ts +0 -3
- package/src/model-api/types.ts +0 -139
- package/src/plugins/__tests__/adapter-registry.test.ts +0 -92
- package/src/plugins/__tests__/lifecycle.test.ts +0 -96
- package/src/plugins/__tests__/loader.test.ts +0 -273
- package/src/plugins/__tests__/validator.test.ts +0 -275
- package/src/plugins/adapter-registry.ts +0 -42
- package/src/plugins/define.ts +0 -5
- package/src/plugins/index.ts +0 -28
- package/src/plugins/lifecycle.ts +0 -27
- package/src/plugins/loader.ts +0 -126
- package/src/plugins/types.ts +0 -76
- package/src/plugins/validator.ts +0 -141
- package/src/schema/__tests__/registry-models-by-module.test.ts +0 -58
- package/src/schema/registry.ts +0 -93
- package/src/schema/relationships.ts +0 -93
- package/src/schema/types.ts +0 -43
- package/src/services/__tests__/integration.test.ts +0 -63
- package/src/services/__tests__/registry.test.ts +0 -175
- package/src/services/index.ts +0 -13
- package/src/services/registry.ts +0 -156
- package/src/services/types.ts +0 -27
- package/src/validation/__tests__/field-validator.test.ts +0 -195
- package/src/validation/field-validator.ts +0 -113
- package/src/validation/index.ts +0 -1
- package/src/widgets/index.ts +0 -3
- package/src/widgets/slot-validator.ts +0 -87
- package/src/widgets/widget-registry.ts +0 -32
- package/tests/boot.test.ts +0 -323
- package/tests/dependency-sort.test.ts +0 -99
- package/tests/discovery.test.ts +0 -126
- package/tests/registry.test.ts +0 -216
- package/tests/schema-loader.test.ts +0 -52
- package/tests/schema-merger.test.ts +0 -180
- package/tsconfig.json +0 -9
- package/tsconfig.tsbuildinfo +0 -1
- package/vitest.config.ts +0 -14
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect, beforeEach } from 'vitest';
|
|
2
|
-
import { WidgetRegistry } from '../widgets/widget-registry.js';
|
|
3
|
-
import { validatePageBody } from '../widgets/slot-validator.js';
|
|
4
|
-
import type { WidgetDefinitionMeta, WidgetNode } from '@rangka/shared';
|
|
5
|
-
|
|
6
|
-
const buttonMeta: WidgetDefinitionMeta = {
|
|
7
|
-
name: 'button',
|
|
8
|
-
label: 'Button',
|
|
9
|
-
category: 'action',
|
|
10
|
-
schema: { label: { type: 'string', required: true } },
|
|
11
|
-
binding: 'none',
|
|
12
|
-
triggers: ['click'],
|
|
13
|
-
container: false,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const inputMeta: WidgetDefinitionMeta = {
|
|
17
|
-
name: 'input',
|
|
18
|
-
label: 'Input',
|
|
19
|
-
category: 'input',
|
|
20
|
-
schema: { label: { type: 'string' } },
|
|
21
|
-
binding: 'field',
|
|
22
|
-
triggers: ['change', 'focus', 'blur'],
|
|
23
|
-
container: false,
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const groupMeta: WidgetDefinitionMeta = {
|
|
27
|
-
name: 'group',
|
|
28
|
-
label: 'Group',
|
|
29
|
-
category: 'layout',
|
|
30
|
-
schema: { direction: { type: 'enum', options: ['row', 'column'] } },
|
|
31
|
-
binding: 'none',
|
|
32
|
-
triggers: [],
|
|
33
|
-
container: true,
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const tableMeta: WidgetDefinitionMeta = {
|
|
37
|
-
name: 'table',
|
|
38
|
-
label: 'Table',
|
|
39
|
-
category: 'layout',
|
|
40
|
-
schema: {},
|
|
41
|
-
binding: 'model',
|
|
42
|
-
triggers: ['rowClick'],
|
|
43
|
-
container: true,
|
|
44
|
-
accepts: ['column'],
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const columnMeta: WidgetDefinitionMeta = {
|
|
48
|
-
name: 'column',
|
|
49
|
-
label: 'Column',
|
|
50
|
-
category: 'layout',
|
|
51
|
-
schema: { label: { type: 'string', required: true } },
|
|
52
|
-
binding: 'none',
|
|
53
|
-
triggers: [],
|
|
54
|
-
container: true,
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
describe('WidgetRegistry', () => {
|
|
58
|
-
let registry: WidgetRegistry;
|
|
59
|
-
|
|
60
|
-
beforeEach(() => {
|
|
61
|
-
registry = new WidgetRegistry();
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test('registers and retrieves a widget', () => {
|
|
65
|
-
registry.register(buttonMeta);
|
|
66
|
-
expect(registry.get('button')).toEqual(buttonMeta);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test('has returns true for registered widget', () => {
|
|
70
|
-
registry.register(buttonMeta);
|
|
71
|
-
expect(registry.has('button')).toBe(true);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test('has returns false for unregistered widget', () => {
|
|
75
|
-
expect(registry.has('nonexistent')).toBe(false);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test('get returns undefined for unregistered widget', () => {
|
|
79
|
-
expect(registry.get('nonexistent')).toBeUndefined();
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test('throws on duplicate registration', () => {
|
|
83
|
-
registry.register(buttonMeta);
|
|
84
|
-
expect(() => registry.register(buttonMeta)).toThrow("Widget 'button' is already registered");
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test('getAll returns all registered widgets', () => {
|
|
88
|
-
registry.register(buttonMeta);
|
|
89
|
-
registry.register(inputMeta);
|
|
90
|
-
expect(registry.getAll()).toHaveLength(2);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
test('names returns all widget names', () => {
|
|
94
|
-
registry.register(buttonMeta);
|
|
95
|
-
registry.register(inputMeta);
|
|
96
|
-
expect(registry.names().sort()).toEqual(['button', 'input']);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test('clear removes all widgets', () => {
|
|
100
|
-
registry.register(buttonMeta);
|
|
101
|
-
registry.clear();
|
|
102
|
-
expect(registry.has('button')).toBe(false);
|
|
103
|
-
expect(registry.getAll()).toHaveLength(0);
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
describe('validatePageBody', () => {
|
|
108
|
-
let widgetRegistry: WidgetRegistry;
|
|
109
|
-
|
|
110
|
-
beforeEach(() => {
|
|
111
|
-
widgetRegistry = new WidgetRegistry();
|
|
112
|
-
widgetRegistry.register(buttonMeta);
|
|
113
|
-
widgetRegistry.register(inputMeta);
|
|
114
|
-
widgetRegistry.register(groupMeta);
|
|
115
|
-
widgetRegistry.register(tableMeta);
|
|
116
|
-
widgetRegistry.register(columnMeta);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test('valid body produces no errors', () => {
|
|
120
|
-
const body: WidgetNode[] = [
|
|
121
|
-
{ type: 'input', bind: { field: 'name' } },
|
|
122
|
-
{ type: 'button', props: { label: 'Save' }, on: { click: { type: 'navigate', path: '/' } } },
|
|
123
|
-
];
|
|
124
|
-
expect(validatePageBody(body, widgetRegistry)).toEqual([]);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
test('empty body is valid', () => {
|
|
128
|
-
expect(validatePageBody([], widgetRegistry)).toEqual([]);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
test('reports unknown widget type', () => {
|
|
132
|
-
const body: WidgetNode[] = [{ type: 'unknown_widget' }];
|
|
133
|
-
const errors = validatePageBody(body, widgetRegistry);
|
|
134
|
-
expect(errors).toHaveLength(1);
|
|
135
|
-
expect(errors[0].path).toBe('body[0]');
|
|
136
|
-
expect(errors[0].message).toContain('Unknown widget type');
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
test('reports children on non-container', () => {
|
|
140
|
-
const body: WidgetNode[] = [
|
|
141
|
-
{ type: 'button', props: { label: 'x' }, children: [{ type: 'input' }] },
|
|
142
|
-
];
|
|
143
|
-
const errors = validatePageBody(body, widgetRegistry);
|
|
144
|
-
expect(errors.some((e) => e.message.includes('not a container'))).toBe(true);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
test('reports disallowed child type', () => {
|
|
148
|
-
const body: WidgetNode[] = [
|
|
149
|
-
{
|
|
150
|
-
type: 'table',
|
|
151
|
-
bind: { model: { name: 'test' } },
|
|
152
|
-
children: [{ type: 'button', props: { label: 'bad' } }],
|
|
153
|
-
},
|
|
154
|
-
];
|
|
155
|
-
const errors = validatePageBody(body, widgetRegistry);
|
|
156
|
-
expect(errors.some((e) => e.message.includes('does not accept child type'))).toBe(true);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
test('reports unknown trigger', () => {
|
|
160
|
-
const body: WidgetNode[] = [
|
|
161
|
-
{ type: 'button', props: { label: 'x' }, on: { hover: { type: 'navigate', path: '/' } } },
|
|
162
|
-
];
|
|
163
|
-
const errors = validatePageBody(body, widgetRegistry);
|
|
164
|
-
expect(errors.some((e) => e.message.includes("Unknown trigger 'hover'"))).toBe(true);
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
test('reports invalid binding mode', () => {
|
|
168
|
-
const body: WidgetNode[] = [{ type: 'button', props: { label: 'x' }, bind: { field: 'name' } }];
|
|
169
|
-
const errors = validatePageBody(body, widgetRegistry);
|
|
170
|
-
expect(errors.some((e) => e.message.includes('does not support data binding'))).toBe(true);
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
test('validates nested children recursively', () => {
|
|
174
|
-
const body: WidgetNode[] = [
|
|
175
|
-
{
|
|
176
|
-
type: 'group',
|
|
177
|
-
children: [
|
|
178
|
-
{
|
|
179
|
-
type: 'group',
|
|
180
|
-
children: [{ type: 'nonexistent' }],
|
|
181
|
-
},
|
|
182
|
-
],
|
|
183
|
-
},
|
|
184
|
-
];
|
|
185
|
-
const errors = validatePageBody(body, widgetRegistry);
|
|
186
|
-
expect(errors).toHaveLength(1);
|
|
187
|
-
expect(errors[0].path).toContain('children');
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
test('validates multiple top-level widgets independently', () => {
|
|
191
|
-
const body: WidgetNode[] = [{ type: 'unknown1' }, { type: 'unknown2' }];
|
|
192
|
-
const errors = validatePageBody(body, widgetRegistry);
|
|
193
|
-
expect(errors).toHaveLength(2);
|
|
194
|
-
expect(errors[0].path).toBe('body[0]');
|
|
195
|
-
expect(errors[1].path).toBe('body[1]');
|
|
196
|
-
});
|
|
197
|
-
});
|
|
@@ -1,389 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
-
import { createServer } from '../server.js';
|
|
3
|
-
import { generateRoutes } from '../route-generator.js';
|
|
4
|
-
import type { SchemaRegistry } from '../../schema/registry.js';
|
|
5
|
-
import type { DatabaseClient } from '../../db/client.js';
|
|
6
|
-
import type { ResolvedModel } from '../../schema/types.js';
|
|
7
|
-
|
|
8
|
-
function makeModel(module: string, name: string): ResolvedModel {
|
|
9
|
-
return {
|
|
10
|
-
qualifiedName: `${module}.${name}`,
|
|
11
|
-
app: 'test',
|
|
12
|
-
module,
|
|
13
|
-
name,
|
|
14
|
-
auditLog: false,
|
|
15
|
-
traits: [],
|
|
16
|
-
fields: [
|
|
17
|
-
{ name: 'id', config: { type: 'string' }, provenance: { source: 'base' } },
|
|
18
|
-
{ name: 'name', config: { type: 'string', required: true }, provenance: { source: 'base' } },
|
|
19
|
-
{
|
|
20
|
-
name: 'amount',
|
|
21
|
-
config: { type: 'decimal', precision: 10, scale: 2 },
|
|
22
|
-
provenance: { source: 'base' },
|
|
23
|
-
},
|
|
24
|
-
],
|
|
25
|
-
indexes: [],
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function makeSoftDeleteModel(module: string, name: string): ResolvedModel {
|
|
30
|
-
return {
|
|
31
|
-
...makeModel(module, name),
|
|
32
|
-
traits: ['soft_delete'],
|
|
33
|
-
fields: [
|
|
34
|
-
...makeModel(module, name).fields,
|
|
35
|
-
{
|
|
36
|
-
name: 'archived_at',
|
|
37
|
-
config: { type: 'datetime' },
|
|
38
|
-
provenance: { source: 'trait', trait: 'soft_delete' },
|
|
39
|
-
},
|
|
40
|
-
],
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function makeRegistry(models: ResolvedModel[]): SchemaRegistry {
|
|
45
|
-
const modelMap = new Map<string, ResolvedModel>();
|
|
46
|
-
for (const m of models) modelMap.set(m.qualifiedName, m);
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
getModelsByModule: () => {
|
|
50
|
-
const map = new Map<string, ResolvedModel[]>();
|
|
51
|
-
for (const m of models) {
|
|
52
|
-
const list = map.get(m.module) ?? [];
|
|
53
|
-
list.push(m);
|
|
54
|
-
map.set(m.module, list);
|
|
55
|
-
}
|
|
56
|
-
return map;
|
|
57
|
-
},
|
|
58
|
-
getRelationshipsForModel: () => [],
|
|
59
|
-
getModel: (qn: string) => modelMap.get(qn),
|
|
60
|
-
getAllModels: () => models,
|
|
61
|
-
getFieldsForModel: (qn: string) => modelMap.get(qn)?.fields ?? [],
|
|
62
|
-
} as unknown as SchemaRegistry;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function makeMockDb(): DatabaseClient {
|
|
66
|
-
const records: Record<string, any[]> = {};
|
|
67
|
-
|
|
68
|
-
function applyWhereFilters(data: any[], wheres: any[]): any[] {
|
|
69
|
-
for (const w of wheres) {
|
|
70
|
-
if (w.op === '=' && w.field !== 'id') {
|
|
71
|
-
data = data.filter((r) => r[w.field] === w.value);
|
|
72
|
-
} else if (w.op === 'is' && w.value === null) {
|
|
73
|
-
data = data.filter((r) => r[w.field] == null);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return data;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const buildQuery = (table: string) => {
|
|
80
|
-
const q: any = {
|
|
81
|
-
_table: table,
|
|
82
|
-
_wheres: [] as any[],
|
|
83
|
-
_selects: [] as string[],
|
|
84
|
-
_selectAll: false,
|
|
85
|
-
_offset: 0,
|
|
86
|
-
_limit: 25,
|
|
87
|
-
};
|
|
88
|
-
q.select = vi.fn((...args: any[]) => {
|
|
89
|
-
q._selects.push(...args);
|
|
90
|
-
return q;
|
|
91
|
-
});
|
|
92
|
-
q.selectAll = vi.fn(() => {
|
|
93
|
-
q._selectAll = true;
|
|
94
|
-
return q;
|
|
95
|
-
});
|
|
96
|
-
q.where = vi.fn((field: string, op: string, value: any) => {
|
|
97
|
-
q._wheres.push({ field, op, value });
|
|
98
|
-
return q;
|
|
99
|
-
});
|
|
100
|
-
q.orderBy = vi.fn(() => q);
|
|
101
|
-
q.offset = vi.fn((n: number) => {
|
|
102
|
-
q._offset = n;
|
|
103
|
-
return q;
|
|
104
|
-
});
|
|
105
|
-
q.limit = vi.fn((n: number) => {
|
|
106
|
-
q._limit = n;
|
|
107
|
-
return q;
|
|
108
|
-
});
|
|
109
|
-
q.execute = vi.fn(async () => {
|
|
110
|
-
let data = records[table] ?? [];
|
|
111
|
-
data = applyWhereFilters(data, q._wheres);
|
|
112
|
-
return data.slice(q._offset, q._offset + q._limit);
|
|
113
|
-
});
|
|
114
|
-
q.executeTakeFirst = vi.fn(async () => {
|
|
115
|
-
const idFilter = q._wheres.find((w: any) => w.field === 'id');
|
|
116
|
-
if (idFilter) {
|
|
117
|
-
let data = records[table] ?? [];
|
|
118
|
-
data = applyWhereFilters(data, q._wheres);
|
|
119
|
-
return data.find((r: any) => r.id === idFilter.value);
|
|
120
|
-
}
|
|
121
|
-
let data = records[table] ?? [];
|
|
122
|
-
data = applyWhereFilters(data, q._wheres);
|
|
123
|
-
return { count: String(data.length) };
|
|
124
|
-
});
|
|
125
|
-
q.executeTakeFirstOrThrow = vi.fn(async () => {
|
|
126
|
-
const result = await q.executeTakeFirst();
|
|
127
|
-
if (!result) throw new Error('No result');
|
|
128
|
-
return result;
|
|
129
|
-
});
|
|
130
|
-
q.values = vi.fn((body: any) => {
|
|
131
|
-
const rec = { id: 'new-id', ...body };
|
|
132
|
-
if (!records[table]) records[table] = [];
|
|
133
|
-
records[table].push(rec);
|
|
134
|
-
return q;
|
|
135
|
-
});
|
|
136
|
-
q.set = vi.fn((body: any) => {
|
|
137
|
-
const idFilter = q._wheres.find((w: any) => w.field === 'id');
|
|
138
|
-
if (idFilter) {
|
|
139
|
-
const data = records[table] ?? [];
|
|
140
|
-
const idx = data.findIndex((r: any) => r.id === idFilter.value);
|
|
141
|
-
if (idx >= 0) data[idx] = { ...data[idx], ...body };
|
|
142
|
-
}
|
|
143
|
-
return q;
|
|
144
|
-
});
|
|
145
|
-
q.returningAll = vi.fn(() => q);
|
|
146
|
-
return q;
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
const db: any = {
|
|
150
|
-
selectFrom: vi.fn((model: string) => buildQuery(model)),
|
|
151
|
-
insertInto: vi.fn((model: string) => buildQuery(model)),
|
|
152
|
-
updateTable: vi.fn((model: string) => buildQuery(model)),
|
|
153
|
-
deleteFrom: vi.fn((model: string) => buildQuery(model)),
|
|
154
|
-
kysely: { fn: { countAll: () => ({ as: (alias: string) => `count_${alias}` }) } },
|
|
155
|
-
_records: records,
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
return db as unknown as DatabaseClient;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
describe('CRUD handlers via route generation', () => {
|
|
162
|
-
let server: Awaited<ReturnType<typeof createServer>>;
|
|
163
|
-
let db: ReturnType<typeof makeMockDb>;
|
|
164
|
-
let registry: ReturnType<typeof makeRegistry>;
|
|
165
|
-
|
|
166
|
-
beforeEach(async () => {
|
|
167
|
-
const models = [makeModel('sales', 'invoice')];
|
|
168
|
-
registry = makeRegistry(models);
|
|
169
|
-
db = makeMockDb();
|
|
170
|
-
server = await createServer();
|
|
171
|
-
generateRoutes(server, registry, db);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
describe('GET /api/sales/invoice (list)', () => {
|
|
175
|
-
it('returns paginated response', async () => {
|
|
176
|
-
(db as any)._records['sales.invoice'] = [
|
|
177
|
-
{ id: '1', name: 'INV-001', amount: 100 },
|
|
178
|
-
{ id: '2', name: 'INV-002', amount: 200 },
|
|
179
|
-
];
|
|
180
|
-
|
|
181
|
-
const res = await server.inject({ method: 'GET', url: '/api/sales/invoice' });
|
|
182
|
-
expect(res.statusCode).toBe(200);
|
|
183
|
-
const body = JSON.parse(res.body);
|
|
184
|
-
expect(body.data).toBeDefined();
|
|
185
|
-
expect(body.meta).toBeDefined();
|
|
186
|
-
expect(body.meta.page).toBe(1);
|
|
187
|
-
expect(body.meta.limit).toBe(25);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
it('returns 400 for invalid filter field', async () => {
|
|
191
|
-
const res = await server.inject({
|
|
192
|
-
method: 'GET',
|
|
193
|
-
url: '/api/sales/invoice?filter[nonexistent][eq]=x',
|
|
194
|
-
});
|
|
195
|
-
expect(res.statusCode).toBe(400);
|
|
196
|
-
const body = JSON.parse(res.body);
|
|
197
|
-
expect(body.error.code).toBe('QUERY_VALIDATION_ERROR');
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
describe('GET /api/sales/invoice/:id (get)', () => {
|
|
202
|
-
it('returns record when found', async () => {
|
|
203
|
-
(db as any)._records['sales.invoice'] = [{ id: '1', name: 'INV-001', amount: 100 }];
|
|
204
|
-
|
|
205
|
-
const res = await server.inject({ method: 'GET', url: '/api/sales/invoice/1' });
|
|
206
|
-
expect(res.statusCode).toBe(200);
|
|
207
|
-
const body = JSON.parse(res.body);
|
|
208
|
-
expect(body.data.id).toBe('1');
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
it('returns 404 when not found', async () => {
|
|
212
|
-
(db as any)._records['sales.invoice'] = [];
|
|
213
|
-
|
|
214
|
-
const res = await server.inject({ method: 'GET', url: '/api/sales/invoice/missing' });
|
|
215
|
-
expect(res.statusCode).toBe(404);
|
|
216
|
-
const body = JSON.parse(res.body);
|
|
217
|
-
expect(body.error.code).toBe('NOT_FOUND');
|
|
218
|
-
});
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
describe('POST /api/sales/invoice (create)', () => {
|
|
222
|
-
it('creates record and returns 201', async () => {
|
|
223
|
-
const res = await server.inject({
|
|
224
|
-
method: 'POST',
|
|
225
|
-
url: '/api/sales/invoice',
|
|
226
|
-
headers: { 'content-type': 'application/json' },
|
|
227
|
-
payload: JSON.stringify({ name: 'INV-003', amount: 500 }),
|
|
228
|
-
});
|
|
229
|
-
expect(res.statusCode).toBe(201);
|
|
230
|
-
const body = JSON.parse(res.body);
|
|
231
|
-
expect(body.data).toBeDefined();
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
it('returns 400 for missing required field', async () => {
|
|
235
|
-
const res = await server.inject({
|
|
236
|
-
method: 'POST',
|
|
237
|
-
url: '/api/sales/invoice',
|
|
238
|
-
headers: { 'content-type': 'application/json' },
|
|
239
|
-
payload: JSON.stringify({ amount: 500 }),
|
|
240
|
-
});
|
|
241
|
-
expect(res.statusCode).toBe(400);
|
|
242
|
-
const body = JSON.parse(res.body);
|
|
243
|
-
expect(body.error.code).toBe('VALIDATION_ERROR');
|
|
244
|
-
expect(body.error.details).toContain('name');
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
it('returns 400 for empty body', async () => {
|
|
248
|
-
const res = await server.inject({
|
|
249
|
-
method: 'POST',
|
|
250
|
-
url: '/api/sales/invoice',
|
|
251
|
-
headers: { 'content-type': 'application/json' },
|
|
252
|
-
payload: '',
|
|
253
|
-
});
|
|
254
|
-
expect(res.statusCode).toBeGreaterThanOrEqual(400);
|
|
255
|
-
});
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
describe('PUT /api/sales/invoice/:id (update)', () => {
|
|
259
|
-
it('updates record when found', async () => {
|
|
260
|
-
(db as any)._records['sales.invoice'] = [{ id: '1', name: 'INV-001', amount: 100 }];
|
|
261
|
-
|
|
262
|
-
const res = await server.inject({
|
|
263
|
-
method: 'PUT',
|
|
264
|
-
url: '/api/sales/invoice/1',
|
|
265
|
-
headers: { 'content-type': 'application/json' },
|
|
266
|
-
payload: JSON.stringify({ amount: 999 }),
|
|
267
|
-
});
|
|
268
|
-
expect(res.statusCode).toBe(200);
|
|
269
|
-
const body = JSON.parse(res.body);
|
|
270
|
-
expect(body.data).toBeDefined();
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
it('returns 404 when record not found', async () => {
|
|
274
|
-
(db as any)._records['sales.invoice'] = [];
|
|
275
|
-
|
|
276
|
-
const res = await server.inject({
|
|
277
|
-
method: 'PUT',
|
|
278
|
-
url: '/api/sales/invoice/missing',
|
|
279
|
-
headers: { 'content-type': 'application/json' },
|
|
280
|
-
payload: JSON.stringify({ amount: 999 }),
|
|
281
|
-
});
|
|
282
|
-
expect(res.statusCode).toBe(404);
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
describe('DELETE /api/sales/invoice/:id (delete)', () => {
|
|
287
|
-
it('returns 204 when record exists', async () => {
|
|
288
|
-
(db as any)._records['sales.invoice'] = [{ id: '1', name: 'INV-001', amount: 100 }];
|
|
289
|
-
|
|
290
|
-
const res = await server.inject({ method: 'DELETE', url: '/api/sales/invoice/1' });
|
|
291
|
-
expect(res.statusCode).toBe(204);
|
|
292
|
-
expect(res.body).toBe('');
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
it('returns 404 when record not found', async () => {
|
|
296
|
-
(db as any)._records['sales.invoice'] = [];
|
|
297
|
-
|
|
298
|
-
const res = await server.inject({ method: 'DELETE', url: '/api/sales/invoice/missing' });
|
|
299
|
-
expect(res.statusCode).toBe(404);
|
|
300
|
-
});
|
|
301
|
-
});
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
describe('Soft-delete behavior', () => {
|
|
305
|
-
let server: Awaited<ReturnType<typeof createServer>>;
|
|
306
|
-
let db: ReturnType<typeof makeMockDb>;
|
|
307
|
-
let registry: ReturnType<typeof makeRegistry>;
|
|
308
|
-
|
|
309
|
-
beforeEach(async () => {
|
|
310
|
-
const models = [makeSoftDeleteModel('sales', 'invoice')];
|
|
311
|
-
registry = makeRegistry(models);
|
|
312
|
-
db = makeMockDb();
|
|
313
|
-
server = await createServer();
|
|
314
|
-
generateRoutes(server, registry, db);
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
describe('DELETE with soft_delete trait', () => {
|
|
318
|
-
it('sets archived_at instead of deleting the record', async () => {
|
|
319
|
-
(db as any)._records['sales.invoice'] = [
|
|
320
|
-
{ id: '1', name: 'INV-001', amount: 100, archived_at: null },
|
|
321
|
-
];
|
|
322
|
-
|
|
323
|
-
const res = await server.inject({ method: 'DELETE', url: '/api/sales/invoice/1' });
|
|
324
|
-
expect(res.statusCode).toBe(204);
|
|
325
|
-
expect(db.updateTable).toHaveBeenCalledWith('sales.invoice');
|
|
326
|
-
expect(db.deleteFrom).not.toHaveBeenCalled();
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
it('returns 404 for already-archived record', async () => {
|
|
330
|
-
(db as any)._records['sales.invoice'] = [
|
|
331
|
-
{ id: '1', name: 'INV-001', amount: 100, archived_at: '2026-01-01T00:00:00.000Z' },
|
|
332
|
-
];
|
|
333
|
-
|
|
334
|
-
const res = await server.inject({ method: 'DELETE', url: '/api/sales/invoice/1' });
|
|
335
|
-
expect(res.statusCode).toBe(404);
|
|
336
|
-
});
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
describe('GET list with soft_delete trait', () => {
|
|
340
|
-
it('excludes archived records by default', async () => {
|
|
341
|
-
(db as any)._records['sales.invoice'] = [
|
|
342
|
-
{ id: '1', name: 'INV-001', amount: 100, archived_at: null },
|
|
343
|
-
{ id: '2', name: 'INV-002', amount: 200, archived_at: '2026-01-01T00:00:00.000Z' },
|
|
344
|
-
];
|
|
345
|
-
|
|
346
|
-
const res = await server.inject({ method: 'GET', url: '/api/sales/invoice' });
|
|
347
|
-
expect(res.statusCode).toBe(200);
|
|
348
|
-
const body = JSON.parse(res.body);
|
|
349
|
-
expect(body.data.every((r: any) => r.archived_at === null)).toBe(true);
|
|
350
|
-
});
|
|
351
|
-
|
|
352
|
-
it('includes archived records when includeArchived=true', async () => {
|
|
353
|
-
(db as any)._records['sales.invoice'] = [
|
|
354
|
-
{ id: '1', name: 'INV-001', amount: 100, archived_at: null },
|
|
355
|
-
{ id: '2', name: 'INV-002', amount: 200, archived_at: '2026-01-01T00:00:00.000Z' },
|
|
356
|
-
];
|
|
357
|
-
|
|
358
|
-
const res = await server.inject({
|
|
359
|
-
method: 'GET',
|
|
360
|
-
url: '/api/sales/invoice?includeArchived=true',
|
|
361
|
-
});
|
|
362
|
-
expect(res.statusCode).toBe(200);
|
|
363
|
-
const body = JSON.parse(res.body);
|
|
364
|
-
expect(body.data).toHaveLength(2);
|
|
365
|
-
});
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
describe('GET by ID with soft_delete trait', () => {
|
|
369
|
-
it('returns 404 for archived record', async () => {
|
|
370
|
-
(db as any)._records['sales.invoice'] = [
|
|
371
|
-
{ id: '1', name: 'INV-001', amount: 100, archived_at: '2026-01-01T00:00:00.000Z' },
|
|
372
|
-
];
|
|
373
|
-
|
|
374
|
-
const res = await server.inject({ method: 'GET', url: '/api/sales/invoice/1' });
|
|
375
|
-
expect(res.statusCode).toBe(404);
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
it('returns active record normally', async () => {
|
|
379
|
-
(db as any)._records['sales.invoice'] = [
|
|
380
|
-
{ id: '1', name: 'INV-001', amount: 100, archived_at: null },
|
|
381
|
-
];
|
|
382
|
-
|
|
383
|
-
const res = await server.inject({ method: 'GET', url: '/api/sales/invoice/1' });
|
|
384
|
-
expect(res.statusCode).toBe(200);
|
|
385
|
-
const body = JSON.parse(res.body);
|
|
386
|
-
expect(body.data.id).toBe('1');
|
|
387
|
-
});
|
|
388
|
-
});
|
|
389
|
-
});
|