@beechcms/api 0.4.3 → 0.6.0-preview.1
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/Searching.svg +1 -0
- package/assets/dashboard/assets/index-BSjk0Sx9.js +634 -0
- package/assets/dashboard/assets/index-kRCYVN2B.css +1 -0
- package/assets/dashboard/index.html +2 -2
- package/assets/dashboard/noResult.svg +43 -0
- package/assets/dashboard/working.svg +1 -0
- package/migrations/0000_v040_base.sql +27 -0
- package/migrations/0030_test_seeds.sql +125 -0
- package/package.json +14 -9
- package/src/auth/{in-memory-hash-provider.ts → __fixtures__/in-memory-hash-provider.ts} +4 -0
- package/src/auth/{static-token-service.ts → __fixtures__/static-token-service.ts} +4 -0
- package/src/auth/bcrypt-hash-provider.ts +6 -2
- package/src/auth/constants.ts +4 -0
- package/src/auth/generate-refresh-token.test.ts +8 -4
- package/src/auth/hash-provider.test.ts +14 -5
- package/src/auth/jose-token-service.ts +7 -0
- package/src/auth/jwt-claims-passthrough.test.ts +87 -0
- package/src/auth/login.test.ts +9 -1
- package/src/auth/login.ts +5 -1
- package/src/auth/refresh.ts +7 -1
- package/src/auth/token-service.test.ts +9 -1
- package/src/factory.ts +37 -16
- package/src/features/automations/__tests__/action-executors.test.ts +59 -72
- package/src/features/automations/__tests__/automation-runner.test.ts +4 -0
- package/src/features/automations/__tests__/automation-runner.utils.test.ts +4 -0
- package/src/features/automations/__tests__/automations.handler.test.ts +7 -3
- package/src/features/automations/__tests__/automations.repository.test.ts +4 -0
- package/src/features/automations/__tests__/automations.schema.test.ts +92 -2
- package/src/features/automations/__tests__/context-resolver.test.ts +4 -0
- package/src/features/automations/__tests__/cron-runner.test.ts +11 -7
- package/src/features/automations/__tests__/cron-runner.utils.test.ts +4 -0
- package/src/features/automations/__tests__/set-variable.executor.test.ts +77 -0
- package/src/features/automations/__tests__/template-grammar.test.ts +4 -0
- package/src/features/automations/__tests__/webhook.executor.test.ts +142 -0
- package/src/features/automations/__tests__/when-evaluator.test.ts +4 -0
- package/src/features/automations/__tests__/when-pushdown.test.ts +5 -1
- package/src/features/automations/action-executors/create-entry.executor.ts +4 -0
- package/src/features/automations/action-executors/edit-field.executor.ts +4 -0
- package/src/features/automations/action-executors/index.ts +5 -1
- package/src/features/automations/action-executors/send-mail.executor.ts +13 -2
- package/src/features/automations/action-executors/set-variable.executor.ts +15 -2
- package/src/features/automations/action-executors/webhook.executor.ts +55 -13
- package/src/features/automations/automation-runner.ts +4 -0
- package/src/features/automations/automation-runner.utils.ts +4 -0
- package/src/features/automations/automations.handler.ts +4 -0
- package/src/features/automations/automations.schema.ts +19 -2
- package/src/features/automations/context-resolver.ts +4 -0
- package/src/features/automations/cron-runner.ts +4 -0
- package/src/features/automations/cron-runner.utils.ts +4 -0
- package/src/features/automations/filter-translation.ts +4 -0
- package/src/features/automations/index.ts +4 -0
- package/src/features/automations/template-grammar.ts +4 -0
- package/src/features/automations/var-access-resolver.ts +4 -0
- package/src/features/automations/when-evaluator.ts +4 -0
- package/src/features/automations/when-pushdown.ts +4 -0
- package/src/features/backrefs/__tests__/backrefs.handler.test.ts +359 -0
- package/src/features/backrefs/backrefs.handler.ts +168 -0
- package/src/features/backrefs/d1-backref.repository.ts +84 -0
- package/src/features/backrefs/index.ts +5 -0
- package/src/features/content/constants.ts +6 -0
- package/src/features/content/handlers/bulk.handler.ts +185 -0
- package/src/features/content/handlers/create.ts +19 -55
- package/src/features/content/handlers/delete.ts +12 -37
- package/src/features/content/handlers/facets.ts +4 -0
- package/src/features/content/handlers/get.ts +4 -0
- package/src/features/content/handlers/helpers.test.ts +180 -0
- package/src/features/content/handlers/helpers.ts +93 -0
- package/src/features/content/handlers/list.ts +105 -19
- package/src/features/content/handlers/update.ts +19 -64
- package/src/features/content/index.ts +6 -0
- package/src/features/draft/draft.handler.ts +33 -8
- package/src/features/draft/draft.middleware.ts +4 -0
- package/src/features/draft/index.ts +4 -0
- package/src/features/email/email.provider.ts +4 -0
- package/src/features/email/email.service.ts +50 -45
- package/src/features/email/email.types.ts +12 -1
- package/src/features/email/index.ts +4 -0
- package/src/features/email/providers/resend.ts +4 -0
- package/src/features/email/providers/smtp.ts +55 -0
- package/src/features/email/templates/automation-mail.ts +4 -0
- package/src/features/email/templates/password-changed.ts +4 -0
- package/src/features/email/templates/password-reset.ts +4 -0
- package/src/features/email/templates/shell.ts +4 -0
- package/src/features/notifications/index.ts +4 -0
- package/src/features/notifications/notifications.handler.ts +4 -0
- package/src/features/password-reset/index.ts +5 -1
- package/src/features/password-reset/request.ts +12 -2
- package/src/features/password-reset/reset.ts +12 -2
- package/src/features/rotate-field/index.ts +4 -0
- package/src/features/rotate-field/rotate-field.handler.ts +4 -0
- package/src/features/rotate-field/rotate-field.schema.ts +4 -0
- package/src/features/schema/schema.handler.ts +118 -3
- package/src/features/seeds/index.ts +5 -0
- package/src/features/seeds/seeds.handler.test.ts +632 -0
- package/src/features/seeds/seeds.handler.ts +693 -0
- package/src/features/settings/__tests__/settings.handler.test.ts +555 -0
- package/src/features/settings/settings.handler.ts +98 -8
- package/src/features/setup/index.ts +124 -11
- package/src/features/stats/index.ts +4 -0
- package/src/features/stats/stats.handler.ts +11 -21
- package/src/features/webhooks/index.ts +63 -0
- package/src/index.ts +28 -19
- package/src/media-utils.ts +4 -0
- package/src/middleware/auth-providers.middleware.ts +13 -0
- package/src/middleware/observability.middleware.ts +21 -3
- package/src/middleware/rate-limit.middleware.ts +4 -0
- package/src/middleware/repository.middleware.ts +25 -2
- package/src/middleware/seed-registry.middleware.test.ts +97 -0
- package/src/middleware/seed-registry.middleware.ts +21 -0
- package/src/middleware/storage.middleware.ts +4 -0
- package/src/middleware.ts +5 -7
- package/src/public/access-policy.ts +4 -0
- package/src/public/api-key-middleware.ts +4 -0
- package/src/public/cache-utils.ts +38 -34
- package/src/public/entry-projection.ts +46 -42
- package/src/public/idempotency.ts +23 -19
- package/src/public/index.ts +4 -0
- package/src/public/problem-details.ts +71 -0
- package/src/public/public-add.ts +4 -0
- package/src/public/public-edit.ts +4 -0
- package/src/public/public-errors.ts +4 -0
- package/src/public/public-read.ts +4 -0
- package/src/public/public-routes.ts +4 -0
- package/src/public/query-builder.test.ts +4 -0
- package/src/public/query-builder.ts +4 -0
- package/src/public/rate-limit-middleware.ts +4 -0
- package/src/public/read-list.ts +54 -50
- package/src/public/read-single.ts +48 -44
- package/src/public/response-builder.ts +4 -0
- package/src/public/sanitize.ts +4 -0
- package/src/public/slug-utils.ts +4 -0
- package/src/rate-limit/cloudflare-rate-limiter.test.ts +4 -0
- package/src/rate-limit/cloudflare-rate-limiter.ts +4 -0
- package/src/rate-limit/in-memory-rate-limiter.test.ts +4 -0
- package/src/rate-limit/in-memory-rate-limiter.ts +4 -0
- package/src/rate-limit/no-op-rate-limiter.test.ts +4 -0
- package/src/rate-limit/no-op-rate-limiter.ts +4 -0
- package/src/search-utils.test.ts +4 -0
- package/src/search-utils.ts +5 -1
- package/src/search.ts +5 -1
- package/src/shared/apply-policies.test.ts +4 -0
- package/src/shared/apply-policies.ts +4 -0
- package/src/shared/automations.repository.d1.ts +4 -0
- package/src/shared/background-notification-service.test.ts +4 -0
- package/src/shared/background-notification-service.ts +4 -0
- package/src/shared/base.repository.d1.ts +4 -0
- package/src/shared/content-utils.test.ts +4 -0
- package/src/shared/content-utils.ts +4 -0
- package/src/shared/content.repository.d1.test.ts +147 -1
- package/src/shared/content.repository.d1.ts +535 -66
- package/src/shared/d1-activity-log.repository.test.ts +4 -0
- package/src/shared/d1-activity-log.repository.ts +4 -0
- package/src/shared/d1-activity-logger.test.ts +4 -0
- package/src/shared/d1-activity-logger.ts +4 -0
- package/src/shared/d1-analytics.repository.test.ts +4 -0
- package/src/shared/d1-analytics.repository.ts +4 -0
- package/src/shared/d1-content-scan.repository.test.ts +9 -5
- package/src/shared/d1-content-scan.repository.ts +15 -7
- package/src/shared/d1-notification.repository.test.ts +4 -0
- package/src/shared/d1-notification.repository.ts +4 -0
- package/src/shared/d1-password-reset-token.repository.test.ts +4 -0
- package/src/shared/d1-password-reset-token.repository.ts +4 -0
- package/src/shared/d1-search.repository.test.ts +4 -0
- package/src/shared/d1-search.repository.ts +4 -0
- package/src/shared/d1-session.repository.test.ts +4 -0
- package/src/shared/d1-session.repository.ts +4 -0
- package/src/shared/d1-setup-checklist.repository.ts +36 -0
- package/src/shared/d1-user.repository.test.ts +8 -4
- package/src/shared/d1-user.repository.ts +15 -5
- package/src/shared/d1-widget.repository.test.ts +4 -0
- package/src/shared/d1-widget.repository.ts +4 -0
- package/src/shared/demo-data-sql.ts +54 -0
- package/src/shared/demo-data.repository.d1.ts +20 -0
- package/src/shared/execution-context-scheduler.ts +4 -0
- package/src/shared/fixed-clock.ts +4 -0
- package/src/shared/fts-sync.ts +4 -0
- package/src/shared/idempotency.repository.d1.test.ts +4 -0
- package/src/shared/idempotency.repository.d1.ts +4 -0
- package/src/shared/in-memory-activity-logger.ts +4 -0
- package/src/shared/in-memory-notification-service.ts +4 -0
- package/src/shared/in-memory-seed.repository.ts +51 -0
- package/src/shared/media.repository.d1.test.ts +4 -0
- package/src/shared/media.repository.d1.ts +4 -0
- package/src/shared/qstash-notification-service.test.ts +67 -0
- package/src/shared/qstash-notification-service.ts +55 -0
- package/src/shared/query-utils.ts +4 -0
- package/src/shared/request-utils.ts +4 -0
- package/src/shared/schema-mutator.d1.ts +64 -0
- package/src/shared/seed-layout.repository.d1.test.ts +114 -0
- package/src/shared/seed-layout.repository.d1.ts +62 -0
- package/src/shared/seed-registry-cache.test.ts +68 -0
- package/src/shared/seed-registry-cache.ts +49 -0
- package/src/shared/seed.repository.d1.test.ts +143 -0
- package/src/shared/seed.repository.d1.ts +98 -0
- package/src/shared/sequential-id-generator.ts +11 -0
- package/src/shared/site-settings.repository.d1.ts +53 -0
- package/src/shared/storage/factory.ts +16 -15
- package/src/shared/storage/s3-bucket.ts +34 -2
- package/src/shared/storage-utils.ts +4 -0
- package/src/shared/system-stats.repository.d1.test.ts +4 -0
- package/src/shared/system-stats.repository.d1.ts +5 -1
- package/src/types.ts +23 -3
- package/src/upload.ts +134 -123
- package/src/widget.ts +6 -2
- package/assets/dashboard/assets/index-BKWnlvnV.css +0 -1
- package/assets/dashboard/assets/index-BMkd1Irh.js +0 -629
- package/src/shared/storage/r2-binding-bucket.ts +0 -81
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
// SPDX-License-Identifier: BUSL-1.1
|
|
2
|
+
// Copyright (c) 2024–2026 Flavio De Musso. All rights reserved.
|
|
3
|
+
// See LICENSE in the repository root for license terms.
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
6
|
+
import { Hono } from 'hono'
|
|
7
|
+
import type { ISeedRepository, ISchemaMutator, Seed, SeedRecord } from '@beechcms/core'
|
|
8
|
+
import { seedsApp } from './seeds.handler'
|
|
9
|
+
|
|
10
|
+
// ─── Fixtures ────────────────────────────────────────────────────────────────
|
|
11
|
+
|
|
12
|
+
const baseSeed: Seed = {
|
|
13
|
+
slug: 'articles',
|
|
14
|
+
label: 'Articles',
|
|
15
|
+
displayNameAlias: 'title',
|
|
16
|
+
branches: [
|
|
17
|
+
{ id: 'br_01', alias: 'title', label: 'Title', type: 'text' },
|
|
18
|
+
{ id: 'br_02', alias: 'body', label: 'Body', type: 'richtext' },
|
|
19
|
+
],
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const baseRecord: SeedRecord = {
|
|
23
|
+
slug: 'articles',
|
|
24
|
+
definition: baseSeed,
|
|
25
|
+
status: 'active',
|
|
26
|
+
source: 'runtime',
|
|
27
|
+
createdAt: 0,
|
|
28
|
+
updatedAt: 0,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ─── Fake repositories ────────────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
function makeRepo(overrides: Partial<ISeedRepository> = {}): ISeedRepository {
|
|
34
|
+
return {
|
|
35
|
+
listActive: vi.fn().mockResolvedValue([baseSeed]),
|
|
36
|
+
listAll: vi.fn().mockResolvedValue([baseRecord]),
|
|
37
|
+
get: vi.fn().mockResolvedValue(null),
|
|
38
|
+
upsert: vi.fn().mockResolvedValue(undefined),
|
|
39
|
+
softDelete: vi.fn().mockResolvedValue(undefined),
|
|
40
|
+
hardDelete: vi.fn().mockResolvedValue(undefined),
|
|
41
|
+
getRegistryVersion: vi.fn().mockResolvedValue(1),
|
|
42
|
+
bumpRegistryVersion: vi.fn().mockResolvedValue(2),
|
|
43
|
+
...overrides,
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function makeMutator(overrides: Partial<ISchemaMutator> = {}): ISchemaMutator {
|
|
48
|
+
return {
|
|
49
|
+
getColumns: vi.fn().mockResolvedValue(null),
|
|
50
|
+
execDdl: vi.fn().mockResolvedValue(undefined),
|
|
51
|
+
dropTable: vi.fn().mockResolvedValue(undefined),
|
|
52
|
+
dropColumn: vi.fn().mockResolvedValue(undefined),
|
|
53
|
+
renameColumn: vi.fn().mockResolvedValue(undefined),
|
|
54
|
+
execDestructive: vi.fn().mockResolvedValue(undefined),
|
|
55
|
+
...overrides,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function makeActivityLogger() {
|
|
60
|
+
return { log: vi.fn() }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ─── App builder ─────────────────────────────────────────────────────────────
|
|
64
|
+
|
|
65
|
+
function buildApp(opts: {
|
|
66
|
+
role?: string
|
|
67
|
+
repo?: ISeedRepository
|
|
68
|
+
mutator?: ISchemaMutator
|
|
69
|
+
backrefMap?: Map<string, any[]>
|
|
70
|
+
automationRepository?: any
|
|
71
|
+
db?: any
|
|
72
|
+
} = {}) {
|
|
73
|
+
const app = new Hono()
|
|
74
|
+
const repo = opts.repo ?? makeRepo()
|
|
75
|
+
const mutator = opts.mutator ?? makeMutator()
|
|
76
|
+
const backrefMap = opts.backrefMap ?? new Map()
|
|
77
|
+
const activityLogger = makeActivityLogger()
|
|
78
|
+
// Default stub DB (used by hard-delete R2 cascade) — no results
|
|
79
|
+
const db = opts.db ?? { prepare: () => ({ all: async () => ({ results: [] }) }) }
|
|
80
|
+
const automationRepository = opts.automationRepository ?? { list: async () => [] }
|
|
81
|
+
|
|
82
|
+
app.use('*', async (c, next) => {
|
|
83
|
+
c.set('jwtPayload' as never, opts.role ? { sub: 'u1', email: 'a@b.com', role: opts.role } : null)
|
|
84
|
+
c.set('seedRepository' as never, repo)
|
|
85
|
+
c.set('schemaMutator' as never, mutator)
|
|
86
|
+
c.set('backrefMap' as never, backrefMap)
|
|
87
|
+
c.set('activityLogger' as never, activityLogger)
|
|
88
|
+
c.set('automationRepository' as never, automationRepository)
|
|
89
|
+
c.set('env' as never, { ENV: 'test', DB: db })
|
|
90
|
+
await next()
|
|
91
|
+
})
|
|
92
|
+
app.route('/', seedsApp)
|
|
93
|
+
return { app, repo, mutator, activityLogger }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ─── Tests ────────────────────────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
describe('GET /', () => {
|
|
99
|
+
it('403 for non-admin', async () => {
|
|
100
|
+
const { app } = buildApp({ role: 'user' })
|
|
101
|
+
const res = await app.request('/', { method: 'GET' })
|
|
102
|
+
expect(res.status).toBe(403)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('200 returns all records', async () => {
|
|
106
|
+
const { app } = buildApp({ role: 'admin' })
|
|
107
|
+
const res = await app.request('/', { method: 'GET' })
|
|
108
|
+
expect(res.status).toBe(200)
|
|
109
|
+
const body = await res.json() as any[]
|
|
110
|
+
expect(body).toHaveLength(1)
|
|
111
|
+
expect(body[0].slug).toBe('articles')
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
describe('GET /:slug', () => {
|
|
116
|
+
it('404 for missing seed', async () => {
|
|
117
|
+
const { app } = buildApp({ role: 'admin' })
|
|
118
|
+
const res = await app.request('/nope', { method: 'GET' })
|
|
119
|
+
expect(res.status).toBe(404)
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it('200 returns the record', async () => {
|
|
123
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
124
|
+
const { app } = buildApp({ role: 'admin', repo })
|
|
125
|
+
const res = await app.request('/articles', { method: 'GET' })
|
|
126
|
+
expect(res.status).toBe(200)
|
|
127
|
+
const body = await res.json() as any
|
|
128
|
+
expect(body.slug).toBe('articles')
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
describe('POST / (create)', () => {
|
|
133
|
+
it('403 for non-admin', async () => {
|
|
134
|
+
const { app } = buildApp({ role: 'user' })
|
|
135
|
+
const res = await app.request('/', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(baseSeed) })
|
|
136
|
+
expect(res.status).toBe(403)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('409 when slug already active', async () => {
|
|
140
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
141
|
+
const { app } = buildApp({ role: 'admin', repo })
|
|
142
|
+
const res = await app.request('/', {
|
|
143
|
+
method: 'POST',
|
|
144
|
+
headers: { 'Content-Type': 'application/json' },
|
|
145
|
+
body: JSON.stringify(baseSeed),
|
|
146
|
+
})
|
|
147
|
+
expect(res.status).toBe(409)
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('creates new seed: planCreateSeed DDL emitted, upsert called, 201', async () => {
|
|
151
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(null), listActive: vi.fn().mockResolvedValue([]) })
|
|
152
|
+
const mutator = makeMutator({ getColumns: vi.fn().mockResolvedValue(null) })
|
|
153
|
+
const { app, activityLogger } = buildApp({ role: 'admin', repo, mutator })
|
|
154
|
+
|
|
155
|
+
const newSeed: Seed = {
|
|
156
|
+
slug: 'notes',
|
|
157
|
+
label: 'Notes',
|
|
158
|
+
displayNameAlias: 'title',
|
|
159
|
+
branches: [{ id: 'br_01', alias: 'title', label: 'Title', type: 'text' }],
|
|
160
|
+
}
|
|
161
|
+
const res = await app.request('/', {
|
|
162
|
+
method: 'POST',
|
|
163
|
+
headers: { 'Content-Type': 'application/json' },
|
|
164
|
+
body: JSON.stringify(newSeed),
|
|
165
|
+
})
|
|
166
|
+
expect(res.status).toBe(201)
|
|
167
|
+
|
|
168
|
+
const ddlCalls = (mutator.execDdl as ReturnType<typeof vi.fn>).mock.calls
|
|
169
|
+
expect(ddlCalls.length).toBe(1)
|
|
170
|
+
const stmts: string[] = ddlCalls[0][0]
|
|
171
|
+
expect(stmts.some((s: string) => s.includes('CREATE TABLE') && s.includes('content_notes'))).toBe(true)
|
|
172
|
+
expect(stmts.some((s: string) => s.includes('DROP'))).toBe(false)
|
|
173
|
+
|
|
174
|
+
expect((repo.upsert as ReturnType<typeof vi.fn>).mock.calls.length).toBe(1)
|
|
175
|
+
expect((repo.bumpRegistryVersion as ReturnType<typeof vi.fn>).mock.calls.length).toBe(1)
|
|
176
|
+
expect(activityLogger.log.mock.calls.length).toBe(1)
|
|
177
|
+
expect(activityLogger.log.mock.calls[0][0].action).toBe('create')
|
|
178
|
+
|
|
179
|
+
const body = await res.json() as any
|
|
180
|
+
expect(body.slug).toBe('notes')
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
it('assigns br_NN ids to branches without ids', async () => {
|
|
184
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(null), listActive: vi.fn().mockResolvedValue([]) })
|
|
185
|
+
const mutator = makeMutator()
|
|
186
|
+
const { app } = buildApp({ role: 'admin', repo, mutator })
|
|
187
|
+
|
|
188
|
+
const noIdSeed = {
|
|
189
|
+
slug: 'tags',
|
|
190
|
+
label: 'Tags',
|
|
191
|
+
displayNameAlias: 'name',
|
|
192
|
+
branches: [{ alias: 'name', label: 'Name', type: 'text' }],
|
|
193
|
+
}
|
|
194
|
+
const res = await app.request('/', {
|
|
195
|
+
method: 'POST',
|
|
196
|
+
headers: { 'Content-Type': 'application/json' },
|
|
197
|
+
body: JSON.stringify(noIdSeed),
|
|
198
|
+
})
|
|
199
|
+
expect(res.status).toBe(201)
|
|
200
|
+
const upsertArg = (repo.upsert as ReturnType<typeof vi.fn>).mock.calls[0][1] as Seed
|
|
201
|
+
expect(upsertArg.branches[0].id).toMatch(/^br_\d+$/)
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
it('revives a soft-deleted seed without failing on IF NOT EXISTS', async () => {
|
|
205
|
+
const deletedRecord: SeedRecord = { ...baseRecord, status: 'deleted' }
|
|
206
|
+
const repo = makeRepo({
|
|
207
|
+
get: vi.fn().mockResolvedValue(deletedRecord),
|
|
208
|
+
listActive: vi.fn().mockResolvedValue([]),
|
|
209
|
+
})
|
|
210
|
+
const existingCols = new Set(['id', 'title'])
|
|
211
|
+
const mutator = makeMutator({ getColumns: vi.fn().mockResolvedValue(existingCols) })
|
|
212
|
+
const { app } = buildApp({ role: 'admin', repo, mutator })
|
|
213
|
+
|
|
214
|
+
const res = await app.request('/', {
|
|
215
|
+
method: 'POST',
|
|
216
|
+
headers: { 'Content-Type': 'application/json' },
|
|
217
|
+
body: JSON.stringify(baseSeed),
|
|
218
|
+
})
|
|
219
|
+
expect(res.status).toBe(201)
|
|
220
|
+
// No CREATE TABLE was emitted (table already exists), planExtendSeed path taken
|
|
221
|
+
const stmts: string[] = (mutator.execDdl as ReturnType<typeof vi.fn>).mock.calls[0]?.[0] ?? []
|
|
222
|
+
expect(stmts.every((s: string) => !s.includes('CREATE TABLE IF NOT EXISTS content_articles') || s.includes('IF NOT EXISTS'))).toBe(true)
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it('422 for unknown relation target', async () => {
|
|
226
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(null), listActive: vi.fn().mockResolvedValue([]) })
|
|
227
|
+
const { app } = buildApp({ role: 'admin', repo })
|
|
228
|
+
|
|
229
|
+
const relSeed: Seed = {
|
|
230
|
+
slug: 'posts',
|
|
231
|
+
label: 'Posts',
|
|
232
|
+
displayNameAlias: 'title',
|
|
233
|
+
branches: [
|
|
234
|
+
{ id: 'br_01', alias: 'title', label: 'Title', type: 'text' },
|
|
235
|
+
{ id: 'br_02', alias: 'author', label: 'Author', type: 'relation', targetSeed: 'nonexistent' },
|
|
236
|
+
],
|
|
237
|
+
}
|
|
238
|
+
const res = await app.request('/', {
|
|
239
|
+
method: 'POST',
|
|
240
|
+
headers: { 'Content-Type': 'application/json' },
|
|
241
|
+
body: JSON.stringify(relSeed),
|
|
242
|
+
})
|
|
243
|
+
expect(res.status).toBe(422)
|
|
244
|
+
})
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
describe('PUT /:slug (edit)', () => {
|
|
248
|
+
it('403 for non-admin', async () => {
|
|
249
|
+
const { app } = buildApp({ role: 'user' })
|
|
250
|
+
const res = await app.request('/articles', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(baseSeed) })
|
|
251
|
+
expect(res.status).toBe(403)
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
it('422 on alias rename (same id, different alias)', async () => {
|
|
255
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
256
|
+
const { app } = buildApp({ role: 'admin', repo })
|
|
257
|
+
|
|
258
|
+
const renamed: Seed = {
|
|
259
|
+
...baseSeed,
|
|
260
|
+
branches: [
|
|
261
|
+
{ id: 'br_01', alias: 'headline', label: 'Headline', type: 'text' }, // alias changed
|
|
262
|
+
{ id: 'br_02', alias: 'body', label: 'Body', type: 'richtext' },
|
|
263
|
+
],
|
|
264
|
+
}
|
|
265
|
+
const res = await app.request('/articles', {
|
|
266
|
+
method: 'PUT',
|
|
267
|
+
headers: { 'Content-Type': 'application/json' },
|
|
268
|
+
body: JSON.stringify(renamed),
|
|
269
|
+
})
|
|
270
|
+
expect(res.status).toBe(422)
|
|
271
|
+
const body = await res.json() as any
|
|
272
|
+
expect(body.type).toContain('alias-rename-not-supported')
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
it('422 on branch type change', async () => {
|
|
276
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
277
|
+
const { app } = buildApp({ role: 'admin', repo })
|
|
278
|
+
|
|
279
|
+
const typeChanged: Seed = {
|
|
280
|
+
...baseSeed,
|
|
281
|
+
branches: [
|
|
282
|
+
{ id: 'br_01', alias: 'title', label: 'Title', type: 'number' }, // type changed
|
|
283
|
+
{ id: 'br_02', alias: 'body', label: 'Body', type: 'richtext' },
|
|
284
|
+
],
|
|
285
|
+
}
|
|
286
|
+
const res = await app.request('/articles', {
|
|
287
|
+
method: 'PUT',
|
|
288
|
+
headers: { 'Content-Type': 'application/json' },
|
|
289
|
+
body: JSON.stringify(typeChanged),
|
|
290
|
+
})
|
|
291
|
+
expect(res.status).toBe(422)
|
|
292
|
+
const body = await res.json() as any
|
|
293
|
+
expect(body.type).toContain('branch-type-change-not-supported')
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
it('add branch emits ADD COLUMN, no DROP', async () => {
|
|
297
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
298
|
+
const existingCols = new Set(['id', 'title', 'body'])
|
|
299
|
+
const mutator = makeMutator({ getColumns: vi.fn().mockResolvedValue(existingCols) })
|
|
300
|
+
const { app } = buildApp({ role: 'admin', repo, mutator })
|
|
301
|
+
|
|
302
|
+
const withNew: Seed = {
|
|
303
|
+
...baseSeed,
|
|
304
|
+
branches: [
|
|
305
|
+
...baseSeed.branches,
|
|
306
|
+
{ id: 'br_03', alias: 'summary', label: 'Summary', type: 'text' },
|
|
307
|
+
],
|
|
308
|
+
}
|
|
309
|
+
const res = await app.request('/articles', {
|
|
310
|
+
method: 'PUT',
|
|
311
|
+
headers: { 'Content-Type': 'application/json' },
|
|
312
|
+
body: JSON.stringify(withNew),
|
|
313
|
+
})
|
|
314
|
+
expect(res.status).toBe(200)
|
|
315
|
+
|
|
316
|
+
const stmts: string[] = (mutator.execDdl as ReturnType<typeof vi.fn>).mock.calls[0][0]
|
|
317
|
+
expect(stmts.some((s: string) => s.includes('ADD COLUMN') && s.includes('summary'))).toBe(true)
|
|
318
|
+
expect(stmts.every((s: string) => !s.includes('DROP'))).toBe(true)
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
it('removing a branch: no DROP emitted, column retained', async () => {
|
|
322
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
323
|
+
const existingCols = new Set(['id', 'title', 'body'])
|
|
324
|
+
const mutator = makeMutator({ getColumns: vi.fn().mockResolvedValue(existingCols) })
|
|
325
|
+
const { app } = buildApp({ role: 'admin', repo, mutator })
|
|
326
|
+
|
|
327
|
+
const withRemoved: Seed = {
|
|
328
|
+
...baseSeed,
|
|
329
|
+
branches: [baseSeed.branches[0]], // body removed
|
|
330
|
+
}
|
|
331
|
+
const res = await app.request('/articles', {
|
|
332
|
+
method: 'PUT',
|
|
333
|
+
headers: { 'Content-Type': 'application/json' },
|
|
334
|
+
body: JSON.stringify(withRemoved),
|
|
335
|
+
})
|
|
336
|
+
expect(res.status).toBe(200)
|
|
337
|
+
|
|
338
|
+
const allStmts: string[] = (mutator.execDdl as ReturnType<typeof vi.fn>).mock.calls.flatMap((c: any) => c[0])
|
|
339
|
+
expect(allStmts.every((s: string) => !s.includes('DROP'))).toBe(true)
|
|
340
|
+
})
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
describe('POST /:slug/branches (add branch)', () => {
|
|
344
|
+
it('assigns id and emits ADD COLUMN', async () => {
|
|
345
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
346
|
+
const existingCols = new Set(['id', 'title', 'body'])
|
|
347
|
+
const mutator = makeMutator({ getColumns: vi.fn().mockResolvedValue(existingCols) })
|
|
348
|
+
const { app } = buildApp({ role: 'admin', repo, mutator })
|
|
349
|
+
|
|
350
|
+
const res = await app.request('/articles/branches', {
|
|
351
|
+
method: 'POST',
|
|
352
|
+
headers: { 'Content-Type': 'application/json' },
|
|
353
|
+
body: JSON.stringify({ alias: 'excerpt', label: 'Excerpt', type: 'text' }),
|
|
354
|
+
})
|
|
355
|
+
expect(res.status).toBe(200)
|
|
356
|
+
const body = await res.json() as any
|
|
357
|
+
expect(body.id).toMatch(/^br_\d+$/)
|
|
358
|
+
|
|
359
|
+
const stmts: string[] = (mutator.execDdl as ReturnType<typeof vi.fn>).mock.calls[0][0]
|
|
360
|
+
expect(stmts.some((s: string) => s.includes('ADD COLUMN') && s.includes('excerpt'))).toBe(true)
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
it('column already present: no ADD COLUMN emitted', async () => {
|
|
364
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
365
|
+
const existingCols = new Set(['id', 'title', 'body', 'excerpt'])
|
|
366
|
+
const mutator = makeMutator({ getColumns: vi.fn().mockResolvedValue(existingCols) })
|
|
367
|
+
const { app } = buildApp({ role: 'admin', repo, mutator })
|
|
368
|
+
|
|
369
|
+
const res = await app.request('/articles/branches', {
|
|
370
|
+
method: 'POST',
|
|
371
|
+
headers: { 'Content-Type': 'application/json' },
|
|
372
|
+
body: JSON.stringify({ alias: 'excerpt', label: 'Excerpt', type: 'text' }),
|
|
373
|
+
})
|
|
374
|
+
expect(res.status).toBe(200)
|
|
375
|
+
|
|
376
|
+
const stmts: string[] = (mutator.execDdl as ReturnType<typeof vi.fn>).mock.calls[0]?.[0] ?? []
|
|
377
|
+
expect(stmts.every((s: string) => !s.includes('ADD COLUMN'))).toBe(true)
|
|
378
|
+
})
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
describe('DELETE /:slug', () => {
|
|
382
|
+
it('403 for non-admin', async () => {
|
|
383
|
+
const { app } = buildApp({ role: 'user' })
|
|
384
|
+
const res = await app.request('/articles', { method: 'DELETE' })
|
|
385
|
+
expect(res.status).toBe(403)
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
it('soft-deletes: status flips, table not dropped, version bumped', async () => {
|
|
389
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
390
|
+
const mutator = makeMutator()
|
|
391
|
+
const { app } = buildApp({ role: 'admin', repo, mutator })
|
|
392
|
+
|
|
393
|
+
const res = await app.request('/articles', { method: 'DELETE' })
|
|
394
|
+
expect(res.status).toBe(200)
|
|
395
|
+
|
|
396
|
+
expect((repo.softDelete as ReturnType<typeof vi.fn>).mock.calls.length).toBe(1)
|
|
397
|
+
expect((repo.bumpRegistryVersion as ReturnType<typeof vi.fn>).mock.calls.length).toBe(1)
|
|
398
|
+
|
|
399
|
+
// No DDL involving DROP was emitted
|
|
400
|
+
const allStmts: string[] = (mutator.execDdl as ReturnType<typeof vi.fn>).mock.calls.flatMap((c: any) => c[0])
|
|
401
|
+
expect(allStmts.every((s: string) => !s.includes('DROP'))).toBe(true)
|
|
402
|
+
})
|
|
403
|
+
|
|
404
|
+
it('409 when seed is referenced by another seed relation', async () => {
|
|
405
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
406
|
+
const backrefMap = new Map([['articles', [{ seedSlug: 'comments', branchAlias: 'article' }]]])
|
|
407
|
+
const { app } = buildApp({ role: 'admin', repo, backrefMap })
|
|
408
|
+
|
|
409
|
+
const res = await app.request('/articles', { method: 'DELETE' })
|
|
410
|
+
expect(res.status).toBe(409)
|
|
411
|
+
const body = await res.json() as any
|
|
412
|
+
expect(body.type).toContain('seed-referenced')
|
|
413
|
+
})
|
|
414
|
+
|
|
415
|
+
it('404 for non-existent seed', async () => {
|
|
416
|
+
const { app } = buildApp({ role: 'admin' })
|
|
417
|
+
const res = await app.request('/nope', { method: 'DELETE' })
|
|
418
|
+
expect(res.status).toBe(404)
|
|
419
|
+
})
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
// ─── Sprint 06 Danger Zone routes ────────────────────────────────────────────
|
|
423
|
+
|
|
424
|
+
describe('GET /:slug/orphans', () => {
|
|
425
|
+
it('returns orphan columns not in the definition', async () => {
|
|
426
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
427
|
+
// DB has 'id', 'slug', 'status', 'created_at', 'updated_at', 'title', 'body', 'legacy'
|
|
428
|
+
const dbCols = new Set(['id', 'slug', 'status', 'created_at', 'updated_at', 'title', 'body', 'legacy'])
|
|
429
|
+
const mutator = makeMutator({ getColumns: vi.fn().mockResolvedValue(dbCols) })
|
|
430
|
+
const { app } = buildApp({ role: 'admin', repo, mutator })
|
|
431
|
+
|
|
432
|
+
const res = await app.request('/articles/orphans', { method: 'GET' })
|
|
433
|
+
expect(res.status).toBe(200)
|
|
434
|
+
const body = await res.json() as any
|
|
435
|
+
expect(body.orphans).toContain('legacy')
|
|
436
|
+
expect(body.orphans).not.toContain('title')
|
|
437
|
+
})
|
|
438
|
+
|
|
439
|
+
it('returns empty when DB has no orphans', async () => {
|
|
440
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
441
|
+
const dbCols = new Set(['id', 'slug', 'status', 'created_at', 'updated_at', 'title', 'body'])
|
|
442
|
+
const mutator = makeMutator({ getColumns: vi.fn().mockResolvedValue(dbCols) })
|
|
443
|
+
const { app } = buildApp({ role: 'admin', repo, mutator })
|
|
444
|
+
|
|
445
|
+
const res = await app.request('/articles/orphans', { method: 'GET' })
|
|
446
|
+
expect(res.status).toBe(200)
|
|
447
|
+
const body = await res.json() as any
|
|
448
|
+
expect(body.orphans).toHaveLength(0)
|
|
449
|
+
})
|
|
450
|
+
})
|
|
451
|
+
|
|
452
|
+
describe('DELETE /:slug/hard', () => {
|
|
453
|
+
it('400 on confirm mismatch', async () => {
|
|
454
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
455
|
+
const { app } = buildApp({ role: 'admin', repo })
|
|
456
|
+
|
|
457
|
+
const res = await app.request('/articles/hard', {
|
|
458
|
+
method: 'DELETE',
|
|
459
|
+
headers: { 'Content-Type': 'application/json' },
|
|
460
|
+
body: JSON.stringify({ confirm: 'wrong' }),
|
|
461
|
+
})
|
|
462
|
+
expect(res.status).toBe(400)
|
|
463
|
+
const body = await res.json() as any
|
|
464
|
+
expect(body.type).toContain('confirmation-required')
|
|
465
|
+
})
|
|
466
|
+
|
|
467
|
+
it('409 when seed is referenced', async () => {
|
|
468
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
469
|
+
const backrefMap = new Map([['articles', [{ sourceSlug: 'comments' }]]])
|
|
470
|
+
const { app } = buildApp({ role: 'admin', repo, backrefMap })
|
|
471
|
+
|
|
472
|
+
const res = await app.request('/articles/hard', {
|
|
473
|
+
method: 'DELETE',
|
|
474
|
+
headers: { 'Content-Type': 'application/json' },
|
|
475
|
+
body: JSON.stringify({ confirm: 'articles' }),
|
|
476
|
+
})
|
|
477
|
+
expect(res.status).toBe(409)
|
|
478
|
+
})
|
|
479
|
+
|
|
480
|
+
it('hard delete: execDestructive called, hardDelete called, version bumped, audit logged', async () => {
|
|
481
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
482
|
+
const mutator = makeMutator()
|
|
483
|
+
const { app, activityLogger } = buildApp({ role: 'admin', repo, mutator })
|
|
484
|
+
|
|
485
|
+
const res = await app.request('/articles/hard', {
|
|
486
|
+
method: 'DELETE',
|
|
487
|
+
headers: { 'Content-Type': 'application/json' },
|
|
488
|
+
body: JSON.stringify({ confirm: 'articles' }),
|
|
489
|
+
})
|
|
490
|
+
expect(res.status).toBe(200)
|
|
491
|
+
|
|
492
|
+
expect((mutator.execDestructive as ReturnType<typeof vi.fn>).mock.calls.length).toBeGreaterThan(0)
|
|
493
|
+
// execDdl must NOT have been called for DROP
|
|
494
|
+
const ddlCalls: string[][] = (mutator.execDdl as ReturnType<typeof vi.fn>).mock.calls.flatMap((c: any) => c[0])
|
|
495
|
+
expect(ddlCalls.every((s: string) => !s.toUpperCase().includes('DROP'))).toBe(true)
|
|
496
|
+
|
|
497
|
+
expect((repo.hardDelete as ReturnType<typeof vi.fn>).mock.calls.length).toBe(1)
|
|
498
|
+
expect((repo.bumpRegistryVersion as ReturnType<typeof vi.fn>).mock.calls.length).toBe(1)
|
|
499
|
+
expect(activityLogger.log.mock.calls[0][0].action).toBe('delete')
|
|
500
|
+
expect(activityLogger.log.mock.calls[0][0].details.op).toBe('hard-delete')
|
|
501
|
+
})
|
|
502
|
+
})
|
|
503
|
+
|
|
504
|
+
describe('DELETE /:slug/branches/:branchId', () => {
|
|
505
|
+
it('400 on confirm mismatch', async () => {
|
|
506
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
507
|
+
const { app } = buildApp({ role: 'admin', repo })
|
|
508
|
+
|
|
509
|
+
const res = await app.request('/articles/branches/br_01', {
|
|
510
|
+
method: 'DELETE',
|
|
511
|
+
headers: { 'Content-Type': 'application/json' },
|
|
512
|
+
body: JSON.stringify({ confirm: 'wrong' }),
|
|
513
|
+
})
|
|
514
|
+
expect(res.status).toBe(400)
|
|
515
|
+
})
|
|
516
|
+
|
|
517
|
+
it('drops branch: execDestructive called, definition updated, version bumped', async () => {
|
|
518
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
519
|
+
const mutator = makeMutator()
|
|
520
|
+
const { app } = buildApp({ role: 'admin', repo, mutator })
|
|
521
|
+
|
|
522
|
+
const res = await app.request('/articles/branches/br_02', {
|
|
523
|
+
method: 'DELETE',
|
|
524
|
+
headers: { 'Content-Type': 'application/json' },
|
|
525
|
+
body: JSON.stringify({ confirm: 'articles.body' }),
|
|
526
|
+
})
|
|
527
|
+
expect(res.status).toBe(200)
|
|
528
|
+
|
|
529
|
+
expect((mutator.execDestructive as ReturnType<typeof vi.fn>).mock.calls.length).toBe(1)
|
|
530
|
+
const upsertArg = (repo.upsert as ReturnType<typeof vi.fn>).mock.calls[0][1] as any
|
|
531
|
+
expect(upsertArg.branches.find((b: any) => b.id === 'br_02')).toBeUndefined()
|
|
532
|
+
expect((repo.bumpRegistryVersion as ReturnType<typeof vi.fn>).mock.calls.length).toBe(1)
|
|
533
|
+
})
|
|
534
|
+
|
|
535
|
+
it('404 for unknown branchId', async () => {
|
|
536
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
537
|
+
const { app } = buildApp({ role: 'admin', repo })
|
|
538
|
+
|
|
539
|
+
const res = await app.request('/articles/branches/br_99', {
|
|
540
|
+
method: 'DELETE',
|
|
541
|
+
headers: { 'Content-Type': 'application/json' },
|
|
542
|
+
body: JSON.stringify({ confirm: 'articles.unknown' }),
|
|
543
|
+
})
|
|
544
|
+
expect(res.status).toBe(404)
|
|
545
|
+
})
|
|
546
|
+
})
|
|
547
|
+
|
|
548
|
+
describe('PATCH /:slug/branches/:branchId/rename', () => {
|
|
549
|
+
it('400 on confirm mismatch', async () => {
|
|
550
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
551
|
+
const { app } = buildApp({ role: 'admin', repo })
|
|
552
|
+
|
|
553
|
+
const res = await app.request('/articles/branches/br_01/rename', {
|
|
554
|
+
method: 'PATCH',
|
|
555
|
+
headers: { 'Content-Type': 'application/json' },
|
|
556
|
+
body: JSON.stringify({ newAlias: 'headline', confirm: 'wrong' }),
|
|
557
|
+
})
|
|
558
|
+
expect(res.status).toBe(400)
|
|
559
|
+
})
|
|
560
|
+
|
|
561
|
+
it('renames alias: execDestructive called with RENAME, definition updated, automation warnings returned', async () => {
|
|
562
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
563
|
+
const mutator = makeMutator()
|
|
564
|
+
const automationRepository = { list: vi.fn().mockResolvedValue([{ id: 'auto_1', conditions: 'title' }]) }
|
|
565
|
+
const { app } = buildApp({ role: 'admin', repo, mutator, automationRepository })
|
|
566
|
+
|
|
567
|
+
const res = await app.request('/articles/branches/br_01/rename', {
|
|
568
|
+
method: 'PATCH',
|
|
569
|
+
headers: { 'Content-Type': 'application/json' },
|
|
570
|
+
body: JSON.stringify({ newAlias: 'headline', confirm: 'articles.title' }),
|
|
571
|
+
})
|
|
572
|
+
expect(res.status).toBe(200)
|
|
573
|
+
|
|
574
|
+
const destructiveCalls: string[][] = (mutator.execDestructive as ReturnType<typeof vi.fn>).mock.calls[0][0]
|
|
575
|
+
expect(destructiveCalls.some((s: string) => s.toUpperCase().includes('RENAME COLUMN') || s.toUpperCase().includes('RENAME TO'))).toBe(true)
|
|
576
|
+
|
|
577
|
+
const upsertArg = (repo.upsert as ReturnType<typeof vi.fn>).mock.calls[0][1] as any
|
|
578
|
+
const renamed = upsertArg.branches.find((b: any) => b.id === 'br_01')
|
|
579
|
+
expect(renamed?.alias).toBe('headline')
|
|
580
|
+
|
|
581
|
+
const body = await res.json() as any
|
|
582
|
+
expect(body.affectedAutomations).toContain('auto_1')
|
|
583
|
+
})
|
|
584
|
+
})
|
|
585
|
+
|
|
586
|
+
describe('PATCH /:slug/branches/:branchId/retype', () => {
|
|
587
|
+
it('400 on confirm mismatch', async () => {
|
|
588
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
589
|
+
const { app } = buildApp({ role: 'admin', repo })
|
|
590
|
+
|
|
591
|
+
const res = await app.request('/articles/branches/br_01/retype', {
|
|
592
|
+
method: 'PATCH',
|
|
593
|
+
headers: { 'Content-Type': 'application/json' },
|
|
594
|
+
body: JSON.stringify({ newType: 'number', confirm: 'wrong' }),
|
|
595
|
+
})
|
|
596
|
+
expect(res.status).toBe(400)
|
|
597
|
+
})
|
|
598
|
+
|
|
599
|
+
it('retypes branch: execDestructive called, definition updated, version bumped', async () => {
|
|
600
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
601
|
+
const mutator = makeMutator()
|
|
602
|
+
const { app } = buildApp({ role: 'admin', repo, mutator })
|
|
603
|
+
|
|
604
|
+
const res = await app.request('/articles/branches/br_01/retype', {
|
|
605
|
+
method: 'PATCH',
|
|
606
|
+
headers: { 'Content-Type': 'application/json' },
|
|
607
|
+
body: JSON.stringify({ newType: 'number', confirm: 'articles.title' }),
|
|
608
|
+
})
|
|
609
|
+
expect(res.status).toBe(200)
|
|
610
|
+
|
|
611
|
+
expect((mutator.execDestructive as ReturnType<typeof vi.fn>).mock.calls.length).toBe(1)
|
|
612
|
+
const upsertArg = (repo.upsert as ReturnType<typeof vi.fn>).mock.calls[0][1] as any
|
|
613
|
+
expect(upsertArg.branches.find((b: any) => b.id === 'br_01')?.type).toBe('number')
|
|
614
|
+
expect((repo.bumpRegistryVersion as ReturnType<typeof vi.fn>).mock.calls.length).toBe(1)
|
|
615
|
+
})
|
|
616
|
+
})
|
|
617
|
+
|
|
618
|
+
describe('POST /:slug/fts/rebuild', () => {
|
|
619
|
+
it('calls execDestructive with FTS statements and logs audit', async () => {
|
|
620
|
+
const repo = makeRepo({ get: vi.fn().mockResolvedValue(baseRecord) })
|
|
621
|
+
const mutator = makeMutator()
|
|
622
|
+
const { app, activityLogger } = buildApp({ role: 'admin', repo, mutator })
|
|
623
|
+
|
|
624
|
+
const res = await app.request('/articles/fts/rebuild', { method: 'POST' })
|
|
625
|
+
expect(res.status).toBe(200)
|
|
626
|
+
|
|
627
|
+
expect((mutator.execDestructive as ReturnType<typeof vi.fn>).mock.calls.length).toBe(1)
|
|
628
|
+
const stmts: string[] = (mutator.execDestructive as ReturnType<typeof vi.fn>).mock.calls[0][0]
|
|
629
|
+
expect(stmts.some((s: string) => s.includes('fts_articles'))).toBe(true)
|
|
630
|
+
expect(activityLogger.log.mock.calls[0][0].details.op).toBe('fts-rebuild')
|
|
631
|
+
})
|
|
632
|
+
})
|