diff --git a/.gitignore b/.gitignore index 7c7101f5..dc3422dd 100644 --- a/.gitignore +++ b/.gitignore @@ -21,7 +21,6 @@ package-lock.json # Generated documentation docs/api-reference/ -__*__/ .build CLAUDE.md diff --git a/test/__mocks__/astro-content.ts b/test/__mocks__/astro-content.ts new file mode 100644 index 00000000..ce1b399f --- /dev/null +++ b/test/__mocks__/astro-content.ts @@ -0,0 +1,54 @@ +/** + * Mock for the `astro:content` virtual module. + * + * Implements getCollection/getEntry directly from the pre-built data store, + * bypassing the astro:data-layer-content virtual module which isn't available + * outside of Vite/Astro's build pipeline. + */ +import { readFileSync } from 'node:fs' +import { resolve } from 'node:path' +import * as devalue from 'devalue' + +export { defineCollection, defineLiveCollection } from 'astro/content/runtime' +export { z } from 'astro/zod' + +// Load the data store once at module init time +const raw = readFileSync(resolve('.astro/data-store.json'), 'utf-8') +const collections: Map> = devalue.unflatten(JSON.parse(raw)) + +export async function getCollection(collection: string, filter?: (entry: unknown) => unknown) { + const col = collections.get(collection) + if (!col) { + console.warn(`The collection ${JSON.stringify(collection)} does not exist or is empty.`) + return [] + } + const entries = [...col.values()].map((raw: any) => ({ ...raw, collection })) + return filter ? entries.filter(filter) : entries +} + +export async function getEntry(collectionOrRef: string | { collection: string; id: string }, id?: string) { + const [collection, entryId] = + typeof collectionOrRef === 'object' + ? [collectionOrRef.collection, collectionOrRef.id] + : [collectionOrRef, id!] + return collections.get(collection)?.get(entryId) ?? undefined +} + +export async function getEntries(entries: { collection: string; id: string }[]) { + return Promise.all(entries.map((e) => getEntry(e))) +} + +export function reference(collection: string) { + return (id: string) => ({ collection, id }) +} + +// Stubs for live collections (not used in tests) +export async function getLiveCollection() { + return { entries: [] } +} +export async function getLiveEntry() { + return { error: new Error('getLiveEntry not supported in tests') } +} +export async function render() { + throw new Error('render() not supported in tests') +} diff --git a/vitest.config.ts b/vitest.config.ts index 81a02d8e..d2e8c571 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,8 +1,12 @@ import { getViteConfig } from 'astro/config' +import { resolve } from 'node:path' export default getViteConfig({ test: { include: ['test/**/*.test.ts'], globalSetup: ['test/global-setup.ts'], + alias: { + 'astro:content': resolve('./test/__mocks__/astro-content.ts'), + }, }, })