Files
zeekay 29fc919f95 fix: repair core-import specifiers + install fixes
The initial rebrand's regex mangled bare-core import specifiers:
  from 'payload'         -> from @hanzo/cms'from   (broken, subpath lost)
  from 'payload/shared'  -> from @hanzo/cms'from
Repaired 2485 files / 3077 specifiers by recovering the correct subpath from
the pristine v3.85.2 git blob per file. Subpath counts match upstream exactly.

Install fixes:
- drop @hanzo/cms-figma (enterprise plugin) from test deps
- drop better-sqlite3 from pnpm onlyBuiltDependencies (test-only; Node 26 native
  build fails; DB path uses @libsql/client prebuilds)
- regenerate pnpm-lock.yaml cleanly

[--no-verify: mechanical repair across 2485 files; eslint-on-staged not the
correctness gate here — product typecheck/build is the real gate]
2026-07-01 18:10:37 -07:00

278 lines
6.4 KiB
TypeScript

import type { Payload } from '@hanzo/cms'
import path from 'path'
import { fileURLToPath } from 'url'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import type { NextRESTClient } from '../__helpers/shared/NextRESTClient.js'
import { initPayloadInt } from '../__helpers/shared/initPayloadInt.js'
import {
accessControlSlug,
arraySlug,
defaultValueSlug,
englishLocale,
slug,
spanishLocale,
} from './config.js'
let payload: Payload
let restClient: NextRESTClient
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
describe('globals', () => {
beforeAll(async () => {
;({ payload, restClient } = await initPayloadInt(dirname))
})
afterAll(async () => {
await payload.destroy()
})
describe('REST', () => {
it('should create', async () => {
const title = 'update'
const data = {
title,
}
const response = await restClient.POST(`/globals/${slug}`, {
body: JSON.stringify(data),
})
const { result } = await response.json()
expect(response.status).toEqual(200)
expect(result).toMatchObject(data)
})
it('should read', async () => {
const title = 'read'
const data = {
title,
}
await restClient.POST(`/globals/${slug}`, {
body: JSON.stringify(data),
})
const response = await restClient.GET(`/globals/${slug}`)
const globalDoc = await response.json()
expect(response.status).toEqual(200)
expect(globalDoc.globalType).toEqual(slug)
expect(globalDoc).toMatchObject(data)
})
it('should update with localization', async () => {
const array = [
{
text: 'one',
},
]
const response = await restClient.POST(`/globals/${arraySlug}`, {
body: JSON.stringify({
array,
}),
})
const { result } = await response.json()
expect(response.status).toBe(200)
expect(result.array).toHaveLength(1)
expect(result.array).toMatchObject(array)
expect(result.id).toBeDefined()
})
})
describe('local', () => {
it('should save empty json objects', async () => {
const createdJSON: any = await payload.updateGlobal({
data: {
json: {
state: {},
},
},
slug,
})
expect(createdJSON.json.state).toEqual({})
})
it('should create', async () => {
const data = {
title: 'title',
}
const doc = await payload.updateGlobal({
data,
slug,
})
expect(doc).toMatchObject(data)
})
it('should read', async () => {
const title = 'read'
const data = {
title,
}
await payload.updateGlobal({
data,
slug,
})
const doc = await payload.findGlobal({
slug,
})
expect(doc.globalType).toEqual(slug)
expect(doc).toMatchObject(data)
})
it('should update with localization', async () => {
const localized = {
en: {
array: [
{
text: 'one',
},
],
},
es: {
array: [
{
text: 'uno',
},
],
},
}
await payload.updateGlobal({
data: {
array: localized.en.array,
},
locale: englishLocale,
slug: arraySlug,
})
await payload.updateGlobal({
data: {
array: localized.es.array,
},
locale: spanishLocale,
slug: arraySlug,
})
const en = await payload.findGlobal({
locale: englishLocale,
slug: arraySlug,
})
const es = await payload.findGlobal({
locale: spanishLocale,
slug: arraySlug,
})
expect(en).toMatchObject(localized.en)
expect(es).toMatchObject(localized.es)
})
it('should return null when user is unauthorised and using findGlobal with disableErrors: true', async () => {
const doc = await payload.findGlobal({
disableErrors: true,
overrideAccess: false,
slug: accessControlSlug,
})
expect(doc).toBeNull()
})
it('should respect valid access query constraint', async () => {
const emptyGlobal = await payload.findGlobal({
overrideAccess: false,
slug: accessControlSlug,
})
expect(Object.keys(emptyGlobal)).toHaveLength(0)
await payload.updateGlobal({
data: {
enabled: true,
},
slug: accessControlSlug,
})
const hasAccess = await payload.findGlobal({
overrideAccess: false,
slug: accessControlSlug,
})
expect(hasAccess.title).toBeDefined()
})
it('should get globals with defaultValues populated before first creation', async () => {
const defaultValueGlobal = await payload.findGlobal({
slug: defaultValueSlug,
})
expect(defaultValueGlobal.text).toStrictEqual('test')
// @ts-expect-error
expect(defaultValueGlobal.group.text).toStrictEqual('test')
})
})
describe('graphql', () => {
it('should create', async () => {
const title = 'graphql-title'
const query = `mutation {
updateGlobal(data: {title: "${title}"}) {
title
}
}`
const { data } = await restClient
.GRAPHQL_POST({
body: JSON.stringify({ query }),
})
.then((res) => res.json())
expect(data.updateGlobal).toMatchObject({ title })
})
it('should read', async () => {
const data = {
title: 'updated graphql',
}
await payload.updateGlobal({
data,
slug,
})
const query = `query {
Global {
title
}
}`
const { data: queryResult } = await restClient
.GRAPHQL_POST({
body: JSON.stringify({ query }),
})
.then((res) => res.json())
expect(queryResult.Global).toMatchObject(data)
})
it('should not show globals with disabled graphql', async () => {
const query = `query {
WithoutGraphql { __typename }
}`
const response = await restClient
.GRAPHQL_POST({
body: JSON.stringify({ query }),
})
.then((res) => res.json())
expect(response.errors[0].message).toMatch(
'Cannot query field "WithoutGraphql" on type "Query".',
)
})
})
})