@beechcms/api 0.4.1 → 0.4.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/assets/dashboard/assets/index-BKWnlvnV.css +1 -0
- package/assets/dashboard/assets/index-BMkd1Irh.js +629 -0
- package/assets/dashboard/index.html +2 -2
- package/migrations/0000_v040_base.sql +25 -0
- package/migrations/0029_automations.sql +14 -0
- package/package.json +4 -3
- package/src/factory.ts +12 -4
- package/src/features/automations/__tests__/action-executors.test.ts +268 -0
- package/src/features/automations/__tests__/automation-runner.test.ts +192 -0
- package/src/features/automations/__tests__/automation-runner.utils.test.ts +56 -0
- package/src/features/automations/__tests__/automations.handler.test.ts +260 -0
- package/src/features/automations/__tests__/automations.repository.test.ts +159 -0
- package/src/features/automations/__tests__/automations.schema.test.ts +134 -0
- package/src/features/automations/__tests__/context-resolver.test.ts +122 -0
- package/src/features/automations/__tests__/cron-runner.test.ts +263 -0
- package/src/features/automations/__tests__/cron-runner.utils.test.ts +140 -0
- package/src/features/automations/__tests__/set-variable.executor.test.ts +306 -0
- package/src/features/automations/__tests__/template-grammar.test.ts +304 -0
- package/src/features/automations/__tests__/when-evaluator.test.ts +270 -0
- package/src/features/automations/__tests__/when-pushdown.test.ts +277 -0
- package/src/features/automations/action-executors/create-entry.executor.ts +23 -0
- package/src/features/automations/action-executors/edit-field.executor.ts +22 -0
- package/src/features/automations/action-executors/index.ts +33 -0
- package/src/features/automations/action-executors/send-mail.executor.ts +42 -0
- package/src/features/automations/action-executors/set-variable.executor.ts +146 -0
- package/src/features/automations/action-executors/webhook.executor.ts +25 -0
- package/src/features/automations/automation-runner.ts +81 -0
- package/src/features/automations/automation-runner.utils.ts +43 -0
- package/src/features/automations/automations.handler.ts +193 -0
- package/src/features/automations/automations.schema.ts +160 -0
- package/src/features/automations/context-resolver.ts +148 -0
- package/src/features/automations/cron-runner.ts +136 -0
- package/src/features/automations/cron-runner.utils.ts +40 -0
- package/src/features/automations/filter-translation.ts +42 -0
- package/src/features/automations/index.ts +12 -0
- package/src/features/automations/template-grammar.ts +241 -0
- package/src/features/automations/var-access-resolver.ts +136 -0
- package/src/features/automations/when-evaluator.ts +83 -0
- package/src/features/automations/when-pushdown.ts +53 -0
- package/src/features/content/handlers/create.ts +8 -0
- package/src/features/content/handlers/delete.ts +8 -1
- package/src/features/content/handlers/update.ts +8 -0
- package/src/features/draft/draft.handler.ts +51 -164
- package/src/features/draft/draft.middleware.ts +62 -0
- package/src/features/email/email.service.ts +13 -0
- package/src/features/email/email.types.ts +10 -0
- package/src/features/email/index.ts +2 -1
- package/src/features/email/templates/automation-mail.ts +15 -0
- package/src/features/settings/settings.handler.ts +2 -1
- package/src/index.ts +40 -8
- package/src/middleware/repository.middleware.ts +32 -1
- package/src/public/cache-utils.ts +34 -0
- package/src/public/entry-projection.ts +42 -0
- package/src/public/idempotency.ts +19 -0
- package/src/public/problem-details.ts +5 -0
- package/src/public/public-add.ts +17 -63
- package/src/public/public-read.ts +20 -177
- package/src/public/read-list.ts +50 -0
- package/src/public/read-single.ts +44 -0
- package/src/shared/automations.repository.d1.ts +146 -0
- package/src/shared/d1-content-scan.repository.test.ts +76 -0
- package/src/shared/execution-context-scheduler.ts +9 -0
- package/src/shared/storage-utils.ts +3 -3
- package/src/types.ts +5 -1
- package/src/upload.ts +3 -2
- package/assets/dashboard/assets/index-CH13idU1.js +0 -554
- package/assets/dashboard/assets/index-CewtCjom.css +0 -1
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest'
|
|
2
|
+
import { executeSetVariable } from '../action-executors/set-variable.executor'
|
|
3
|
+
import { resolveAutomationContext } from '../context-resolver'
|
|
4
|
+
import { resolveVarAccess } from '../var-access-resolver'
|
|
5
|
+
import { parseTemplateKey } from '../template-grammar'
|
|
6
|
+
import type { ContentRepository, Seed } from '@beechcms/core'
|
|
7
|
+
|
|
8
|
+
const MOCK_SEED: Seed = {
|
|
9
|
+
slug: 'clienti',
|
|
10
|
+
label: 'Clienti',
|
|
11
|
+
branches: [
|
|
12
|
+
{ alias: 'name', label: 'Name', type: 'text', id: 'br_01' },
|
|
13
|
+
{ alias: 'email', label: 'Email', type: 'text', id: 'br_02' },
|
|
14
|
+
{ alias: 'total', label: 'Total', type: 'number', id: 'br_03' },
|
|
15
|
+
],
|
|
16
|
+
} as unknown as Seed
|
|
17
|
+
|
|
18
|
+
async function makeCtx(overrides: Partial<{
|
|
19
|
+
entry: Record<string, unknown>
|
|
20
|
+
variables: Record<string, unknown>
|
|
21
|
+
repository: ContentRepository
|
|
22
|
+
getSeed: (slug: string) => Seed | null
|
|
23
|
+
seed: Seed
|
|
24
|
+
}> = {}) {
|
|
25
|
+
const entry = overrides.entry ?? { id: 'entry-1', customer_id: 'cust-42' }
|
|
26
|
+
const variables = overrides.variables ?? {}
|
|
27
|
+
const baseContext = await resolveAutomationContext({} as any, entry, [entry])
|
|
28
|
+
const context = {
|
|
29
|
+
triggerEntry: baseContext.triggerEntry,
|
|
30
|
+
lookup(parsed: any, onMissing?: any) {
|
|
31
|
+
if (parsed.kind === 'simple') {
|
|
32
|
+
const varVal = (variables as any)[parsed.path]
|
|
33
|
+
if (varVal !== undefined) return varVal
|
|
34
|
+
if (parsed.path.includes('.')) {
|
|
35
|
+
const [first, ...rest] = parsed.path.split('.')
|
|
36
|
+
const varRoot = (variables as any)[first]
|
|
37
|
+
if (varRoot !== undefined) {
|
|
38
|
+
let cur: any = varRoot
|
|
39
|
+
for (const k of rest) cur = cur?.[k]
|
|
40
|
+
if (cur !== undefined) return cur
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
} else if (parsed.kind === 'var_access') {
|
|
44
|
+
return resolveVarAccess(parsed, variables, onMissing)
|
|
45
|
+
}
|
|
46
|
+
return baseContext.lookup(parsed, onMissing)
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
entry,
|
|
51
|
+
variables,
|
|
52
|
+
repository: overrides.repository ?? {
|
|
53
|
+
findMany: vi.fn().mockResolvedValue({ items: [] }),
|
|
54
|
+
} as unknown as ContentRepository,
|
|
55
|
+
getSeed: overrides.getSeed ?? vi.fn().mockReturnValue(MOCK_SEED),
|
|
56
|
+
seed: overrides.seed ?? MOCK_SEED,
|
|
57
|
+
context,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe('fixed_id mode', () => {
|
|
62
|
+
it('1 — fixed_id set, record exists → variables[name] equals item', async () => {
|
|
63
|
+
const item = { id: 'c_1', name: 'Mario', email: 'mario@x.com', total: 99 }
|
|
64
|
+
const findMany = vi.fn().mockResolvedValue({ items: [item] })
|
|
65
|
+
const ctx = await makeCtx({ repository: { findMany } as unknown as ContentRepository })
|
|
66
|
+
|
|
67
|
+
await executeSetVariable(
|
|
68
|
+
{ type: 'set_variable', name: 'cliente', seed_slug: 'clienti', fixed_id: 'c_1' },
|
|
69
|
+
ctx,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
expect(ctx.variables.cliente).toEqual(item)
|
|
73
|
+
const [, opts] = findMany.mock.calls[0]
|
|
74
|
+
expect(opts.filters[0].conditions[0].value).toBe('c_1')
|
|
75
|
+
expect(opts.pagination.limit).toBe(1)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('2 — fixed_id set, record missing → null + console.warn', async () => {
|
|
79
|
+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
80
|
+
const ctx = await makeCtx({
|
|
81
|
+
repository: { findMany: vi.fn().mockResolvedValue({ items: [] }) } as unknown as ContentRepository,
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
await executeSetVariable(
|
|
85
|
+
{ type: 'set_variable', name: 'cliente', seed_slug: 'clienti', fixed_id: 'ghost' },
|
|
86
|
+
ctx,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
expect(ctx.variables.cliente).toBeNull()
|
|
90
|
+
expect(warnSpy).toHaveBeenCalled()
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('3 — fixed_id + column → { _value: item[column] }', async () => {
|
|
94
|
+
const item = { id: 'c_1', name: 'Mario', email: 'mario@x.com', total: 99 }
|
|
95
|
+
const ctx = await makeCtx({
|
|
96
|
+
repository: { findMany: vi.fn().mockResolvedValue({ items: [item] }) } as unknown as ContentRepository,
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
await executeSetVariable(
|
|
100
|
+
{ type: 'set_variable', name: 'cliente', seed_slug: 'clienti', fixed_id: 'c_1', column: 'email' },
|
|
101
|
+
ctx,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
expect(ctx.variables.cliente).toEqual({ _value: 'mario@x.com' })
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('4 — fixed_id interpolation from this scope (dot notation)', async () => {
|
|
108
|
+
const findMany = vi.fn().mockResolvedValue({ items: [] })
|
|
109
|
+
const ctx = await makeCtx({
|
|
110
|
+
entry: { id: 'e1', customer_id: 'cust-77' },
|
|
111
|
+
repository: { findMany } as unknown as ContentRepository,
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
await executeSetVariable(
|
|
115
|
+
{ type: 'set_variable', name: 'cliente', seed_slug: 'clienti', fixed_id: '{{this.customer_id}}' },
|
|
116
|
+
ctx,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
const [, opts] = findMany.mock.calls[0]
|
|
120
|
+
expect(opts.filters[0].conditions[0].value).toBe('cust-77')
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
describe('collection mode', () => {
|
|
125
|
+
it('5 — no filters: count, firstone, lastone, sum, pluck correct', async () => {
|
|
126
|
+
const items = [
|
|
127
|
+
{ id: '1', name: 'A', email: 'a@x.com', total: 10, created_at: 1000 },
|
|
128
|
+
{ id: '2', name: 'B', email: 'b@x.com', total: 20, created_at: 2000 },
|
|
129
|
+
{ id: '3', name: 'C', email: 'c@x.com', total: 30, created_at: 500 },
|
|
130
|
+
]
|
|
131
|
+
const ctx = await makeCtx({
|
|
132
|
+
repository: { findMany: vi.fn().mockResolvedValue({ items }) } as unknown as ContentRepository,
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
await executeSetVariable(
|
|
136
|
+
{ type: 'set_variable', name: 'ordini', seed_slug: 'clienti' },
|
|
137
|
+
ctx,
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
const result = ctx.variables.ordini as any
|
|
141
|
+
expect(result.count).toBe(3)
|
|
142
|
+
expect(result.sum.total).toBe(60)
|
|
143
|
+
expect(result.firstone).toEqual(items[2])
|
|
144
|
+
expect(result.lastone).toEqual(items[1])
|
|
145
|
+
expect(result.pluck.name).toBe('A, B, C')
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('6 — collection with filters → findMany receives resolved filter groups', async () => {
|
|
149
|
+
const findMany = vi.fn().mockResolvedValue({ items: [] })
|
|
150
|
+
const ctx = await makeCtx({ repository: { findMany } as unknown as ContentRepository })
|
|
151
|
+
|
|
152
|
+
await executeSetVariable(
|
|
153
|
+
{
|
|
154
|
+
type: 'set_variable',
|
|
155
|
+
name: 'ordini',
|
|
156
|
+
seed_slug: 'clienti',
|
|
157
|
+
filters: [{ field: 'name', op: 'eq', value: 'Mario' }],
|
|
158
|
+
},
|
|
159
|
+
ctx,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
const [, opts] = findMany.mock.calls[0]
|
|
163
|
+
expect(opts.filters).toHaveLength(1)
|
|
164
|
+
expect(opts.filters[0].column).toBe('name')
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
it('7 — collection with column pin: scalar aggregates + count non-null', async () => {
|
|
168
|
+
const items = [
|
|
169
|
+
{ id: '1', total: 10, created_at: 1 },
|
|
170
|
+
{ id: '2', total: null, created_at: 2 },
|
|
171
|
+
{ id: '3', total: 20, created_at: 3 },
|
|
172
|
+
]
|
|
173
|
+
const ctx = await makeCtx({
|
|
174
|
+
repository: { findMany: vi.fn().mockResolvedValue({ items }) } as unknown as ContentRepository,
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
await executeSetVariable(
|
|
178
|
+
{ type: 'set_variable', name: 'r', seed_slug: 'clienti', column: 'total' },
|
|
179
|
+
ctx,
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
const result = ctx.variables.r as any
|
|
183
|
+
expect(result.count).toBe(2)
|
|
184
|
+
expect(result.sum).toBe(30)
|
|
185
|
+
expect(result.avg).toBe(15)
|
|
186
|
+
expect(result.min).toBe(10)
|
|
187
|
+
expect(result.max).toBe(20)
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
it('8 — seed_slug omitted → uses ctx.seed.slug', async () => {
|
|
191
|
+
const findMany = vi.fn().mockResolvedValue({ items: [] })
|
|
192
|
+
const triggerSeed: Seed = { ...MOCK_SEED, slug: 'ordini' }
|
|
193
|
+
const ctx = await makeCtx({
|
|
194
|
+
repository: { findMany } as unknown as ContentRepository,
|
|
195
|
+
getSeed: vi.fn().mockReturnValue(triggerSeed),
|
|
196
|
+
seed: triggerSeed,
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
await executeSetVariable(
|
|
200
|
+
{ type: 'set_variable', name: 'result' },
|
|
201
|
+
ctx,
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
const [seed] = findMany.mock.calls[0]
|
|
205
|
+
expect(seed.slug).toBe('ordini')
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
it('9 — seed not found → null + warn', async () => {
|
|
209
|
+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
210
|
+
const ctx = await makeCtx({ getSeed: vi.fn().mockReturnValue(null) })
|
|
211
|
+
|
|
212
|
+
await executeSetVariable(
|
|
213
|
+
{ type: 'set_variable', name: 'x', seed_slug: 'ghost' },
|
|
214
|
+
ctx,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
expect(ctx.variables.x).toBeNull()
|
|
218
|
+
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('ghost'))
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
it('10 — filter value interpolated from a previously set variable', async () => {
|
|
222
|
+
const findMany = vi.fn().mockResolvedValue({ items: [] })
|
|
223
|
+
const ctx = await makeCtx({
|
|
224
|
+
variables: { cliente: { id: 'cust-123' } } as Record<string, unknown>,
|
|
225
|
+
repository: { findMany } as unknown as ContentRepository,
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
await executeSetVariable(
|
|
229
|
+
{
|
|
230
|
+
type: 'set_variable',
|
|
231
|
+
name: 'ordini',
|
|
232
|
+
seed_slug: 'clienti',
|
|
233
|
+
filters: [{ field: 'customer_id', op: 'eq', value: '{{cliente.id}}' }],
|
|
234
|
+
},
|
|
235
|
+
ctx,
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
const [, opts] = findMany.mock.calls[0]
|
|
239
|
+
expect(opts.filters[0].conditions[0].value).toBe('cust-123')
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
it('12 — date branch: sum/avg/min/max computed from ISO strings', async () => {
|
|
243
|
+
const dateSeed: Seed = {
|
|
244
|
+
...MOCK_SEED,
|
|
245
|
+
branches: [
|
|
246
|
+
{ alias: 'publishedAt', label: 'Published', type: 'date', id: 'br_d1' },
|
|
247
|
+
],
|
|
248
|
+
} as unknown as Seed
|
|
249
|
+
const items = [
|
|
250
|
+
{ id: '1', publishedAt: '2025-01-10T00:00:00.000Z', created_at: 1 },
|
|
251
|
+
{ id: '2', publishedAt: '2025-03-05T00:00:00.000Z', created_at: 2 },
|
|
252
|
+
{ id: '3', publishedAt: '2025-06-01T00:00:00.000Z', created_at: 3 },
|
|
253
|
+
]
|
|
254
|
+
const ctx = await makeCtx({
|
|
255
|
+
repository: { findMany: vi.fn().mockResolvedValue({ items }) } as unknown as ContentRepository,
|
|
256
|
+
getSeed: vi.fn().mockReturnValue(dateSeed),
|
|
257
|
+
seed: dateSeed,
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
await executeSetVariable({ type: 'set_variable', name: 'articoli', seed_slug: 'clienti' }, ctx)
|
|
261
|
+
|
|
262
|
+
const result = ctx.variables.articoli as any
|
|
263
|
+
expect(result.sum.publishedAt).toBeGreaterThan(0)
|
|
264
|
+
expect(result.avg.publishedAt).toBeGreaterThan(0)
|
|
265
|
+
expect(result.min.publishedAt).toBeLessThan(result.max.publishedAt)
|
|
266
|
+
const minDate = new Date('2025-01-10T00:00:00.000Z').getTime() / 1000
|
|
267
|
+
const maxDate = new Date('2025-06-01T00:00:00.000Z').getTime() / 1000
|
|
268
|
+
expect(result.min.publishedAt).toBe(minDate)
|
|
269
|
+
expect(result.max.publishedAt).toBe(maxDate)
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
it('13 — var_access inline condition on ISO date field', async () => {
|
|
273
|
+
const items = [
|
|
274
|
+
{ id: '1', publishedAt: '2025-01-10T00:00:00.000Z', created_at: 1 },
|
|
275
|
+
{ id: '2', publishedAt: '2025-03-05T00:00:00.000Z', created_at: 2 },
|
|
276
|
+
{ id: '3', publishedAt: '2026-06-01T00:00:00.000Z', created_at: 3 },
|
|
277
|
+
]
|
|
278
|
+
const ctx = await makeCtx({
|
|
279
|
+
repository: { findMany: vi.fn().mockResolvedValue({ items }) } as unknown as ContentRepository,
|
|
280
|
+
})
|
|
281
|
+
await executeSetVariable({ type: 'set_variable', name: 'arts', seed_slug: 'clienti' }, ctx)
|
|
282
|
+
|
|
283
|
+
const parsed = parseTemplateKey('arts.(publishedAt>1740000000).count')
|
|
284
|
+
expect(parsed?.kind).toBe('var_access')
|
|
285
|
+
const result = ctx.context.lookup(parsed!)
|
|
286
|
+
// 1740000000 = ~2025-02-20; art-0002 (Mar 2025) and art-0003 (Jun 2026) qualify
|
|
287
|
+
expect(result).toBe(2)
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
it('11 — _items is non-enumerable (not in JSON.stringify)', async () => {
|
|
291
|
+
const items = [{ id: '1', name: 'A', email: 'a@x.com', total: 5, created_at: 1 }]
|
|
292
|
+
const ctx = await makeCtx({
|
|
293
|
+
repository: { findMany: vi.fn().mockResolvedValue({ items }) } as unknown as ContentRepository,
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
await executeSetVariable(
|
|
297
|
+
{ type: 'set_variable', name: 'ordini', seed_slug: 'clienti' },
|
|
298
|
+
ctx,
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
const json = JSON.stringify(ctx.variables)
|
|
302
|
+
expect(json).not.toContain('_items')
|
|
303
|
+
const result = ctx.variables.ordini as any
|
|
304
|
+
expect(result._items).toEqual(items)
|
|
305
|
+
})
|
|
306
|
+
})
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { parseTemplateKey } from '../template-grammar'
|
|
3
|
+
import type { ParsedKey, VarStep, InlineCondition } from '../template-grammar'
|
|
4
|
+
|
|
5
|
+
describe('parseTemplateKey', () => {
|
|
6
|
+
describe('simple keys', () => {
|
|
7
|
+
it('returns simple for a plain field name', () => {
|
|
8
|
+
expect(parseTemplateKey('title')).toEqual<ParsedKey>({ kind: 'simple', path: 'title' })
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('returns simple for a dot-path', () => {
|
|
12
|
+
expect(parseTemplateKey('author.name')).toEqual<ParsedKey>({ kind: 'simple', path: 'author.name' })
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('returns simple for _count (legacy)', () => {
|
|
16
|
+
expect(parseTemplateKey('_count')).toEqual<ParsedKey>({ kind: 'simple', path: '_count' })
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('returns null for empty string', () => {
|
|
20
|
+
expect(parseTemplateKey('')).toBeNull()
|
|
21
|
+
})
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
describe('two-token sugar: <scope>:<field>', () => {
|
|
25
|
+
it('defaults to lastone selector for <seedSlug>:<field>', () => {
|
|
26
|
+
expect(parseTemplateKey('customers:email')).toEqual<ParsedKey>({
|
|
27
|
+
kind: 'scoped',
|
|
28
|
+
scope: 'customers',
|
|
29
|
+
selector: { kind: 'lastone' },
|
|
30
|
+
op: 'field',
|
|
31
|
+
field: 'email',
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('defaults to lastone selector for this:<field>', () => {
|
|
36
|
+
expect(parseTemplateKey('this:email')).toEqual<ParsedKey>({
|
|
37
|
+
kind: 'scoped',
|
|
38
|
+
scope: 'this',
|
|
39
|
+
selector: { kind: 'lastone' },
|
|
40
|
+
op: 'field',
|
|
41
|
+
field: 'email',
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('sugar: batch:count → batch:all:count', () => {
|
|
46
|
+
expect(parseTemplateKey('batch:count')).toEqual<ParsedKey>({
|
|
47
|
+
kind: 'scoped',
|
|
48
|
+
scope: 'batch',
|
|
49
|
+
selector: { kind: 'all' },
|
|
50
|
+
op: 'count',
|
|
51
|
+
field: null,
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('sugar: customers:sum → customers:all:sum (aggregate sugar without subfield)', () => {
|
|
56
|
+
const result = parseTemplateKey('customers:sum')
|
|
57
|
+
expect(result).toMatchObject({ kind: 'scoped', scope: 'customers', selector: { kind: 'all' }, op: 'sum' })
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
describe('three-token: <scope>:<selector>:<field>', () => {
|
|
62
|
+
it('lastone selector', () => {
|
|
63
|
+
expect(parseTemplateKey('customers:lastone:email')).toEqual<ParsedKey>({
|
|
64
|
+
kind: 'scoped',
|
|
65
|
+
scope: 'customers',
|
|
66
|
+
selector: { kind: 'lastone' },
|
|
67
|
+
op: 'field',
|
|
68
|
+
field: 'email',
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('firstone selector', () => {
|
|
73
|
+
expect(parseTemplateKey('customers:firstone:name')).toEqual<ParsedKey>({
|
|
74
|
+
kind: 'scoped',
|
|
75
|
+
scope: 'customers',
|
|
76
|
+
selector: { kind: 'firstone' },
|
|
77
|
+
op: 'field',
|
|
78
|
+
field: 'name',
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('byid selector', () => {
|
|
83
|
+
expect(parseTemplateKey('customers:byid(c_42):name')).toEqual<ParsedKey>({
|
|
84
|
+
kind: 'scoped',
|
|
85
|
+
scope: 'customers',
|
|
86
|
+
selector: { kind: 'byid', id: 'c_42' },
|
|
87
|
+
op: 'field',
|
|
88
|
+
field: 'name',
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('where selector', () => {
|
|
93
|
+
expect(parseTemplateKey('orders:where(status=paid):total')).toEqual<ParsedKey>({
|
|
94
|
+
kind: 'scoped',
|
|
95
|
+
scope: 'orders',
|
|
96
|
+
selector: { kind: 'where', alias: 'status', value: 'paid' },
|
|
97
|
+
op: 'field',
|
|
98
|
+
field: 'total',
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('all:count aggregate', () => {
|
|
103
|
+
expect(parseTemplateKey('orders:all:count')).toEqual<ParsedKey>({
|
|
104
|
+
kind: 'scoped',
|
|
105
|
+
scope: 'orders',
|
|
106
|
+
selector: { kind: 'all' },
|
|
107
|
+
op: 'count',
|
|
108
|
+
field: null,
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('all:sum:<subfield> aggregate', () => {
|
|
113
|
+
expect(parseTemplateKey('orders:all:sum:total')).toEqual<ParsedKey>({
|
|
114
|
+
kind: 'scoped',
|
|
115
|
+
scope: 'orders',
|
|
116
|
+
selector: { kind: 'all' },
|
|
117
|
+
op: 'sum',
|
|
118
|
+
field: 'total',
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it('all:avg aggregate', () => {
|
|
123
|
+
expect(parseTemplateKey('orders:all:avg:amount')).toMatchObject({
|
|
124
|
+
kind: 'scoped', scope: 'orders', op: 'avg', field: 'amount',
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('all:min aggregate', () => {
|
|
129
|
+
expect(parseTemplateKey('orders:all:min:price')).toMatchObject({
|
|
130
|
+
kind: 'scoped', scope: 'orders', op: 'min', field: 'price',
|
|
131
|
+
})
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('all:max aggregate', () => {
|
|
135
|
+
expect(parseTemplateKey('orders:all:max:score')).toMatchObject({
|
|
136
|
+
kind: 'scoped', scope: 'orders', op: 'max', field: 'score',
|
|
137
|
+
})
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
it('all:pluck:<subfield> aggregate', () => {
|
|
141
|
+
expect(parseTemplateKey('customers:all:pluck:email')).toEqual<ParsedKey>({
|
|
142
|
+
kind: 'scoped',
|
|
143
|
+
scope: 'customers',
|
|
144
|
+
selector: { kind: 'all' },
|
|
145
|
+
op: 'pluck',
|
|
146
|
+
field: 'email',
|
|
147
|
+
})
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('batch:all:count', () => {
|
|
151
|
+
expect(parseTemplateKey('batch:all:count')).toEqual<ParsedKey>({
|
|
152
|
+
kind: 'scoped',
|
|
153
|
+
scope: 'batch',
|
|
154
|
+
selector: { kind: 'all' },
|
|
155
|
+
op: 'count',
|
|
156
|
+
field: null,
|
|
157
|
+
})
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('batch:all:pluck:email', () => {
|
|
161
|
+
expect(parseTemplateKey('batch:all:pluck:email')).toMatchObject({
|
|
162
|
+
kind: 'scoped', scope: 'batch', op: 'pluck', field: 'email',
|
|
163
|
+
})
|
|
164
|
+
})
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
describe('rejection rules', () => {
|
|
168
|
+
it('returns null when aggregate used without all selector', () => {
|
|
169
|
+
expect(parseTemplateKey('orders:lastone:sum')).toBeNull()
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('returns null when selector token is unrecognised', () => {
|
|
173
|
+
expect(parseTemplateKey('orders:unknown:field')).toBeNull()
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it('returns null when there are no field tokens after selector', () => {
|
|
177
|
+
expect(parseTemplateKey('orders:lastone')).toEqual<ParsedKey>({
|
|
178
|
+
kind: 'scoped',
|
|
179
|
+
scope: 'orders',
|
|
180
|
+
selector: { kind: 'lastone' },
|
|
181
|
+
op: 'field',
|
|
182
|
+
field: 'lastone',
|
|
183
|
+
})
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
it('colons inside parens do not split tokens', () => {
|
|
187
|
+
const result = parseTemplateKey('customers:byid(ns:id-123):name')
|
|
188
|
+
expect(result).toMatchObject({
|
|
189
|
+
kind: 'scoped',
|
|
190
|
+
scope: 'customers',
|
|
191
|
+
selector: { kind: 'byid', id: 'ns:id-123' },
|
|
192
|
+
op: 'field',
|
|
193
|
+
field: 'name',
|
|
194
|
+
})
|
|
195
|
+
})
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
describe('scoped with this scope', () => {
|
|
199
|
+
it('this:lastone:field', () => {
|
|
200
|
+
expect(parseTemplateKey('this:lastone:name')).toMatchObject({
|
|
201
|
+
kind: 'scoped', scope: 'this', selector: { kind: 'lastone' }, op: 'field', field: 'name',
|
|
202
|
+
})
|
|
203
|
+
})
|
|
204
|
+
})
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
// ── Sprint 07 var_access cases ─────────────────────────────────────────────────
|
|
208
|
+
|
|
209
|
+
describe('parseTemplateKey — var_access (Sprint 07)', () => {
|
|
210
|
+
it('1 — ordini.array[a,b].count → var_access with array + agg:count', () => {
|
|
211
|
+
const result = parseTemplateKey('ordini.array[a,b].count')
|
|
212
|
+
expect(result).toEqual<ParsedKey>({
|
|
213
|
+
kind: 'var_access',
|
|
214
|
+
name: 'ordini',
|
|
215
|
+
steps: [
|
|
216
|
+
{ type: 'array', ids: ['a', 'b'] },
|
|
217
|
+
{ type: 'agg', op: 'count' },
|
|
218
|
+
],
|
|
219
|
+
conditions: [],
|
|
220
|
+
})
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
it('2 — ordini.count.(status=paid) → var_access with agg:count + condition', () => {
|
|
224
|
+
const result = parseTemplateKey('ordini.count.(status=paid)')
|
|
225
|
+
expect(result).toEqual<ParsedKey>({
|
|
226
|
+
kind: 'var_access',
|
|
227
|
+
name: 'ordini',
|
|
228
|
+
steps: [{ type: 'agg', op: 'count' }],
|
|
229
|
+
conditions: [{ column: 'status', op: '=', value: 'paid' }],
|
|
230
|
+
})
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
it('3 — ordini.sum.total.(amount>100) → var_access with agg:sum:total + condition', () => {
|
|
234
|
+
const result = parseTemplateKey('ordini.sum.total.(amount>100)')
|
|
235
|
+
expect(result).toEqual<ParsedKey>({
|
|
236
|
+
kind: 'var_access',
|
|
237
|
+
name: 'ordini',
|
|
238
|
+
steps: [{ type: 'agg', op: 'sum', field: 'total' }],
|
|
239
|
+
conditions: [{ column: 'amount', op: '>', value: '100' }],
|
|
240
|
+
})
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
it('4 — ordini.firstone.id.(status=paid) → nav + field + condition', () => {
|
|
244
|
+
const result = parseTemplateKey('ordini.firstone.id.(status=paid)')
|
|
245
|
+
expect(result).toEqual<ParsedKey>({
|
|
246
|
+
kind: 'var_access',
|
|
247
|
+
name: 'ordini',
|
|
248
|
+
steps: [
|
|
249
|
+
{ type: 'nav', nav: 'firstone' },
|
|
250
|
+
{ type: 'field', name: 'id' },
|
|
251
|
+
],
|
|
252
|
+
conditions: [{ column: 'status', op: '=', value: 'paid' }],
|
|
253
|
+
})
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
it('5 — ordini.array[a,b].(status=paid).count → array + condition + agg', () => {
|
|
257
|
+
const result = parseTemplateKey('ordini.array[a,b].(status=paid).count')
|
|
258
|
+
expect(result).toMatchObject({
|
|
259
|
+
kind: 'var_access',
|
|
260
|
+
name: 'ordini',
|
|
261
|
+
steps: [
|
|
262
|
+
{ type: 'array', ids: ['a', 'b'] },
|
|
263
|
+
{ type: 'agg', op: 'count' },
|
|
264
|
+
],
|
|
265
|
+
conditions: [{ column: 'status', op: '=', value: 'paid' }],
|
|
266
|
+
})
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
it('6 — cliente.email → falls through to simple (fast path)', () => {
|
|
270
|
+
const result = parseTemplateKey('cliente.email')
|
|
271
|
+
expect(result).toEqual<ParsedKey>({ kind: 'simple', path: 'cliente.email' })
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
it('handles multiple inline conditions (AND-chained)', () => {
|
|
275
|
+
const result = parseTemplateKey('ordini.count.(status=paid).(amount>10)')
|
|
276
|
+
expect(result).toMatchObject({
|
|
277
|
+
kind: 'var_access',
|
|
278
|
+
conditions: [
|
|
279
|
+
{ column: 'status', op: '=', value: 'paid' },
|
|
280
|
+
{ column: 'amount', op: '>', value: '10' },
|
|
281
|
+
],
|
|
282
|
+
})
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
it('handles != operator', () => {
|
|
286
|
+
const result = parseTemplateKey('ordini.count.(status!=draft)')
|
|
287
|
+
expect(result).toMatchObject({
|
|
288
|
+
kind: 'var_access',
|
|
289
|
+
conditions: [{ column: 'status', op: '!=', value: 'draft' }],
|
|
290
|
+
})
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
it('handles <= and >= operators', () => {
|
|
294
|
+
const r1 = parseTemplateKey('ordini.count.(amount<=100)')
|
|
295
|
+
const r2 = parseTemplateKey('ordini.count.(amount>=50)')
|
|
296
|
+
expect(r1).toMatchObject({ conditions: [{ op: '<=' }] })
|
|
297
|
+
expect(r2).toMatchObject({ conditions: [{ op: '>=' }] })
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
it('plain dot-path without special chars stays simple', () => {
|
|
301
|
+
expect(parseTemplateKey('ordini.count')).toEqual({ kind: 'simple', path: 'ordini.count' })
|
|
302
|
+
expect(parseTemplateKey('cliente.nome')).toEqual({ kind: 'simple', path: 'cliente.nome' })
|
|
303
|
+
})
|
|
304
|
+
})
|