Files
ipal-kit/test/titleSeparator.test.ts
Radosław SmolińskiandClaude Opus 5.5 bb10fb10fd Fix /api/graphql 500: Site settings title separator values are GraphQL enum names
The titleSeparator select stored '|', '–', '-', '·', '/'. Select values become
GraphQL enum names; '|', '–', '·' are invalid and '-' and '/' collide on '_', so
every project's /api/graphql answered 500 since 1.0.1 (admin/REST unaffected).

- Values are now pipe/endash/hyphen/middot/slash (same labels); TITLE_SEPARATORS
  and titleSeparatorChar map them to the character; titles accept old characters
  and custom project values literally
- Field afterRead/beforeChange turn an old stored character into its name
- MongoDB: rewritten at start (migrateTitleSeparator); Postgres: titleSeparatorSql()
  for the generated migration (verified up/down/up against Postgres 16)
- Test builds the full plugin GraphQL schema and fails on invalid or duplicate
  enum names; unit tests for the mapping, hooks, SQL and Mongo rewrite
- CHANGELOG Unreleased (Fixed + upgrade step 5), seo.md, ipal-upgrade, api.md

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-29 12:06:55 +01:00

83 lines
3.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, test, vi } from 'vitest'
import { generalFields } from '../src/globals/SiteSettings/fields/general.js'
import { readSiteMetaConfig } from '../src/modules/seo/readSiteMetaConfig.js'
import {
migrateTitleSeparator,
TITLE_SEPARATORS,
titleSeparatorChar,
titleSeparatorName,
titleSeparatorSql,
} from '../src/modules/seo/titleSeparator.js'
type SelectField = {
defaultValue: string
hooks: { afterRead: ((a: { value: unknown }) => unknown)[]; beforeChange: ((a: { value: unknown }) => unknown)[] }
name: string
options: { value: string }[]
}
const separatorField = (): SelectField => {
const find = (fields: unknown[]): SelectField | undefined => {
for (const f of fields as { fields?: unknown[]; name?: string }[]) {
if (f.name === 'titleSeparator') {return f as unknown as SelectField}
const inner = f.fields && find(f.fields)
if (inner) {return inner}
}
return undefined
}
return find(generalFields)!
}
describe('Site Settings → Title separator (GraphQL-safe values)', () => {
test('stored values are names; each maps to its character', () => {
const field = separatorField()
expect(field.defaultValue).toBe('pipe')
expect(field.options.map((o) => o.value)).toEqual(['pipe', 'endash', 'hyphen', 'middot', 'slash'])
expect(field.options.map((o) => titleSeparatorChar(o.value))).toEqual(['|', '–', '-', '·', '/'])
})
test('legacy characters (≤ 1.14.4) still work; unknown values are used literally', () => {
for (const char of Object.values(TITLE_SEPARATORS)) {expect(titleSeparatorChar(char)).toBe(char)}
expect(titleSeparatorChar('»')).toBe('»')
expect(titleSeparatorChar('')).toBeUndefined()
expect(titleSeparatorChar(null)).toBeUndefined()
expect(titleSeparatorName('|')).toBe('pipe')
expect(titleSeparatorName('endash')).toBe('endash')
expect(titleSeparatorName('»')).toBe('»')
})
test('the field reads and saves a legacy character as its name (no "invalid selection")', () => {
const { hooks } = separatorField()
expect(hooks.afterRead[0]({ value: '·' })).toBe('middot')
expect(hooks.beforeChange[0]({ value: '/' })).toBe('slash')
expect(hooks.beforeChange[0]({ value: 'hyphen' })).toBe('hyphen')
})
test('titles use the character, for names and legacy values', async () => {
const read = (titleSeparator: string) =>
readSiteMetaConfig({ locale: 'pl', payload: {} as never, settings: { homepage: null, siteName: 'Acme', titleSeparator } })
expect((await read('endash')).separator).toBe(' – ')
expect((await read('|')).separator).toBe(' | ')
})
test('migration SQL maps both ways and leaves other values alone', () => {
expect(titleSeparatorSql()).toBe(
`UPDATE "site_settings" SET "title_separator" = CASE "title_separator" WHEN '–' THEN 'endash' WHEN '-' THEN 'hyphen' WHEN '·' THEN 'middot' WHEN '|' THEN 'pipe' WHEN '/' THEN 'slash' ELSE "title_separator" END WHERE "title_separator" IN ('–', '-', '·', '|', '/');`,
)
expect(titleSeparatorSql({ direction: 'down', table: 'settings' })).toContain(`UPDATE "settings" SET "title_separator" = CASE "title_separator" WHEN 'endash' THEN '–'`)
})
test('MongoDB: legacy character rewritten at start; other adapters untouched', async () => {
const updateOne = vi.fn(async (filter: { titleSeparator: string }) => ({ modifiedCount: filter.titleSeparator === '|' ? 1 : 0 }))
const logger = { error: vi.fn(), info: vi.fn() }
await migrateTitleSeparator({ db: { name: 'mongoose', globals: { collection: { updateOne } } }, logger } as never)
expect(updateOne).toHaveBeenCalledWith({ globalType: 'site-settings', titleSeparator: '|' }, { $set: { titleSeparator: 'pipe' } })
expect(logger.info).toHaveBeenCalledOnce()
const sql = vi.fn()
await migrateTitleSeparator({ db: { name: 'postgres', globals: { collection: { updateOne: sql } } }, logger } as never)
expect(sql).not.toHaveBeenCalled()
})
})