Gap fixes: top-level searchSettings, public shouldLoadGoogle, consent/stats collection overrides

- ipalKit({ searchSettings }): switches off / reshapes Site Settings → Search & AI
  without `seo`; read as options.searchSettings ?? options.seo?.searchSettings.
- shouldLoadGoogle moved to a pure module (analytics/shouldLoadGoogle.ts) and
  exported from @intecion/ipal-kit and /client.
- consentLog.collectionOverrides and stats.collectionOverrides (object merges
  access/admin/hooks key by key, function takes over), via the new exported
  applyCollectionOverride / CollectionOverride. The stats view and dashboard
  chart now follow the stats-visits read access, so overrides narrow them too.

All additive; defaults unchanged. Docs: AGENT.md, ipal-reference, consent,
stats, seo, analytics, ipal-setup.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-22 18:54:08 +02:00
co-authored by Claude Opus 5.5
parent 4911da7112
commit 5a07c42f76
22 changed files with 235 additions and 54 deletions
+11 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest'
import { shouldLoadGoogle } from '../src/modules/analytics/Analytics.js'
import { shouldLoadGoogle } from '../src/modules/analytics/shouldLoadGoogle.js'
const none = { analytics: false, marketing: false }
@@ -17,3 +17,13 @@ describe("Google tag and consent", () => {
expect(shouldLoadGoogle({ consentMode: 'basic', ready: false, state: { analytics: true } })).toBe(false)
})
})
describe('shouldLoadGoogle is public', () => {
test('exported from the root and /client, from a module without React', async () => {
const { readFileSync } = await import('node:fs')
const read = (p: string) => readFileSync(new URL(`../${p}`, import.meta.url), 'utf8')
expect(read('src/index.ts')).toMatch(/export \{ shouldLoadGoogle \} from '\.\/modules\/analytics\/shouldLoadGoogle\.js'/)
expect(read('src/exports/client.ts')).toMatch(/export \{ shouldLoadGoogle \} from '\.\.\/modules\/analytics\/shouldLoadGoogle\.js'/)
expect(read('src/modules/analytics/shouldLoadGoogle.ts')).not.toMatch(/^import|'use client'/m)
})
})
+55
View File
@@ -143,3 +143,58 @@ describe('options that keep features reachable and overridable', () => {
expect(read({ req: { payload: { collections: {} }, user: { id: 1, collection: 'other' } } })).toBe(false)
})
})
describe('gap fixes: searchSettings, collection overrides', () => {
const tabsOf = (config: Config) => {
const settings = config.globals!.find((g) => g.slug === 'site-settings')!
const tabs = settings.fields.find((f) => f.type === 'tabs') as { tabs: { label?: unknown }[] }
return tabs.tabs.map((t) => String(t.label))
}
test('searchSettings: false removes the Search & AI tab without `seo`', async () => {
expect(tabsOf(await ipalKit({ i18n, twoFactor: false })(base()))).toContain('Search & AI')
expect(tabsOf(await ipalKit({ i18n, searchSettings: false, twoFactor: false })(base()))).not.toContain('Search & AI')
})
test('searchSettings: a function shapes the fields; the top-level option wins over seo.searchSettings', async () => {
const config = await ipalKit({
i18n,
searchSettings: (fields) => fields.filter((f) => 'name' in f && f.name === 'llmsIntro'),
seo: { collections: [], searchSettings: false },
twoFactor: false,
})(base())
const settings = config.globals!.find((g) => g.slug === 'site-settings')!
const tab = (settings.fields.find((f) => f.type === 'tabs') as { tabs: { fields: { name?: string }[]; label?: unknown }[] }).tabs.find(
(t) => t.label === 'Search & AI',
)!
expect(tab.fields.map((f) => f.name)).toEqual(['llmsIntro'])
// the older seo.searchSettings still works alone
expect(tabsOf(await ipalKit({ i18n, seo: { collections: [], searchSettings: false }, twoFactor: false })(base()))).not.toContain('Search & AI')
})
test('consentLog / stats collectionOverrides: an object merges access, a function takes over', async () => {
const dpoOnly = ({ req }: { req: { user?: { email?: string } } }) => req.user?.email === '[email protected]'
const config = await ipalKit({
consentLog: { collectionOverrides: { access: { read: dpoOnly as never }, admin: { group: 'Privacy' } } },
i18n,
stats: { collectionOverrides: (c) => ({ ...c, labels: { plural: 'Visits', singular: 'Visit' } }) },
twoFactor: false,
})(base())
const consent = config.collections!.find((c) => c.slug === 'consent-records')!
expect(consent.access!.read).toBe(dpoOnly)
expect(consent.access!.create).toBeTypeOf('function') // plugin's rule kept
expect(consent.admin).toMatchObject({ group: 'Privacy', useAsTitle: 'consentId' })
const stats = config.collections!.find((c) => c.slug === 'stats-visits')!
expect(stats.labels).toEqual({ plural: 'Visits', singular: 'Visit' })
expect(stats.access!.read).toBeTypeOf('function')
})
test('the stats view and widget follow the collection read access', async () => {
const { canReadStats } = await import('../src/modules/stats/admin/canReadStats.js')
const payload = (read: unknown) => ({ collections: { 'stats-visits': { config: { access: { read } } } } }) as never
const onlyAnna = ({ req }: { req: { user?: { email?: string } } }) => req.user?.email === '[email protected]'
expect(await canReadStats(payload(onlyAnna), undefined, { email: '[email protected]' })).toBe(true)
expect(await canReadStats(payload(onlyAnna), { user: { email: '[email protected]' } } as never)).toBe(false)
expect(await canReadStats({ collections: {} } as never, undefined, { email: 'x' })).toBe(false)
})
})