Files
cms/test/plugin-cloud-storage/int.compositePrefixes.int.spec.ts
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

116 lines
3.4 KiB
TypeScript

import type { Payload } from '@hanzo/cms'
import type { SuiteAPI } from 'vitest'
import * as AWS from '@aws-sdk/client-s3'
import path from 'path'
import shelljs from 'shelljs'
import { fileURLToPath } from 'url'
import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest'
import { initPayloadInt } from '../__helpers/shared/initPayloadInt.js'
import { collectionPrefix, mediaWithCompositePrefixesSlug } from './shared.js'
import { clearTestBucket, createTestBucket } from './utils.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
function describeIfInCIOrHasLocalstack(): SuiteAPI | SuiteAPI['skip'] {
if (process.env.CI) {
return describe
}
const { code } = shelljs.exec(`docker ps | grep localstack`)
if (code !== 0) {
console.warn('Localstack is not running. Skipping test suite.')
return describe.skip
}
console.log('Localstack is running. Running test suite.')
return describe
}
describe('@hanzo/cms-plugin-cloud-storage (composite prefixes)', () => {
let payload: Payload
let TEST_BUCKET: string
beforeAll(async () => {
;({ payload } = await initPayloadInt(dirname, undefined, true, 'config.compositePrefixes.ts'))
TEST_BUCKET = process.env.S3_BUCKET!
})
afterAll(async () => {
await payload.destroy()
})
let client: AWS.S3Client
describeIfInCIOrHasLocalstack()('S3 composite prefixes', () => {
describe('S3', () => {
beforeAll(async () => {
client = new AWS.S3({
credentials: {
accessKeyId: process.env.S3_ACCESS_KEY_ID!,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
},
endpoint: process.env.S3_ENDPOINT!,
forcePathStyle: process.env.S3_FORCE_PATH_STYLE === 'true',
region: process.env.S3_REGION!,
})
await createTestBucket()
await clearTestBucket(client)
})
afterEach(async () => {
await clearTestBucket(client)
})
it('can upload with composite prefixes (collection + doc prefix)', async () => {
const docPrefix = 'user-123'
const upload = await payload.create({
collection: mediaWithCompositePrefixesSlug,
data: {
prefix: docPrefix,
},
filePath: path.resolve(dirname, '../uploads/image.png'),
})
expect(upload.id).toBeTruthy()
const expectedKey = `${collectionPrefix}/${docPrefix}/${upload.filename}`
const { $metadata } = await client.send(
new AWS.HeadObjectCommand({ Bucket: TEST_BUCKET, Key: expectedKey }),
)
expect($metadata.httpStatusCode).toBe(200)
expect(upload.url).toEqual(
`/api/${mediaWithCompositePrefixesSlug}/file/${String(upload.filename)}?prefix=${docPrefix}`,
)
})
it('can upload with composite prefixes (collection prefix only)', async () => {
const upload = await payload.create({
collection: mediaWithCompositePrefixesSlug,
data: {},
filePath: path.resolve(dirname, '../uploads/image.png'),
})
expect(upload.id).toBeTruthy()
const expectedKey = `${collectionPrefix}/${upload.filename}`
const { $metadata } = await client.send(
new AWS.HeadObjectCommand({ Bucket: TEST_BUCKET, Key: expectedKey }),
)
expect($metadata.httpStatusCode).toBe(200)
})
})
})
})