From 6363bc73f79002572583e767846df9ebdd3eeb33 Mon Sep 17 00:00:00 2001 From: Raphaelle Cantin Date: Wed, 8 Jan 2025 14:48:06 +0000 Subject: [PATCH] Strealine the test scripts to have one source of truth for default IDs. Fix allowedTypes type --- pipeline/src/helpers/getGraphQuery.ts | 12 +- pipeline/src/scripts/documentIds.ts | 26 ++++ pipeline/src/scripts/testGraphQuery.ts | 166 +++++++++------------- pipeline/src/scripts/testTransformer.ts | 122 +++++----------- pipeline/test/update-prismic-snapshots.ts | 12 +- 5 files changed, 145 insertions(+), 193 deletions(-) create mode 100644 pipeline/src/scripts/documentIds.ts diff --git a/pipeline/src/helpers/getGraphQuery.ts b/pipeline/src/helpers/getGraphQuery.ts index 1fd58070..7d463137 100644 --- a/pipeline/src/helpers/getGraphQuery.ts +++ b/pipeline/src/helpers/getGraphQuery.ts @@ -30,13 +30,21 @@ export const allowedTypes = [ 'visual-story', 'project', 'season', -]; +] as const; +export type AddressablesAllowedTypes = (typeof allowedTypes)[number]; + +export const isAddressablesAllowedTypes = ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + type: any +): type is AddressablesAllowedTypes => { + return allowedTypes.includes(type); +}; export const getGraphQuery = ({ type, isDetailed, }: { - type: (typeof allowedTypes)[number]; + type: AddressablesAllowedTypes; isDetailed?: boolean; }) => { switch (type) { diff --git a/pipeline/src/scripts/documentIds.ts b/pipeline/src/scripts/documentIds.ts new file mode 100644 index 00000000..b9ba2bea --- /dev/null +++ b/pipeline/src/scripts/documentIds.ts @@ -0,0 +1,26 @@ +import { AddressablesAllowedTypes } from '@weco/content-pipeline/src/helpers/getGraphQuery'; + +const documentIds: { [key: string]: string | undefined } = { + article: 'ZdSMbREAACQA3j30', + webcomic: 'XkV9dREAAAPkNP0b', + event: 'ZfhSyxgAACQAkLPZ', + venue: 'Wsttgx8AAJeSNmJ4', + exhibition: 'Yzv9ChEAABfUrkVp', + book: 'ZijgihEAACMAtL-', + page: 'YdXSvhAAAIAW7YXQ', + 'visual-story': 'Zs8EuRAAAB4APxrA', + 'exhibition-text': 'Zs8mohAAAB4AP4sc', + 'exhibition-highlight-tour': 'ZthrZRIAACQALvCC', + project: 'Ys1-xEAACEAguyS', + season: 'X84FvhIAACUAqiqp', +}; + +export const getDocumentId = ({ + type, + id, +}: { + type: AddressablesAllowedTypes; + id?: string; +}): string | undefined => { + return id || documentIds[type]; +}; diff --git a/pipeline/src/scripts/testGraphQuery.ts b/pipeline/src/scripts/testGraphQuery.ts index 331fd07d..3b715962 100644 --- a/pipeline/src/scripts/testGraphQuery.ts +++ b/pipeline/src/scripts/testGraphQuery.ts @@ -2,11 +2,15 @@ import util from 'util'; import yargs from 'yargs'; import { + AddressablesAllowedTypes, allowedTypes, getGraphQuery, + isAddressablesAllowedTypes, } from '@weco/content-pipeline/src/helpers/getGraphQuery'; import { createPrismicClient } from '@weco/content-pipeline/src/services/prismic'; +import { getDocumentId } from './documentIds'; + const { type, isDetailed, id } = yargs(process.argv.slice(2)) .usage('Usage: $0 --type [string] --isDetailed [boolean] --id [string]') .options({ @@ -16,129 +20,87 @@ const { type, isDetailed, id } = yargs(process.argv.slice(2)) }) .parseSync(); +const getGraphQueryName = ({ + type, + isDetailed, +}: { + type: AddressablesAllowedTypes; + isDetailed?: boolean; +}) => { + switch (type) { + case 'article': + return isDetailed ? 'articlesQuery' : 'addressablesArticlesQuery'; + + case 'webcomic': + return 'webcomicsQuery'; + + case 'event': + return isDetailed ? 'eventDocumentsQuery' : 'addressablesEventsQuery'; + + case 'venue': + return 'venueQuery'; + + case 'exhibition': + return 'addressablesExhibitionsQuery'; + + case 'book': + return 'addressablesBooksQuery'; + + case 'page': + return 'addressablesPagesQuery'; + + case 'visual-story': + return 'addressablesVisualStoriesQuery'; + + case 'exhibition-text': + return 'addressablesExhibitionTextsQuery'; + + case 'exhibition-highlight-tour': + return 'addressablesExhibitionHighlightToursQuery'; + + case 'project': + return 'addressablesProjectsQuery'; + + case 'season': + return 'addressablesSeasonsQuery'; + + default: + console.error(`Allowed types are ${allowedTypes.join(', ')}.`); + process.exit(1); + } +}; + async function main() { - if (!type) { + if (!isAddressablesAllowedTypes(type)) { console.error( `Please pass in the type you'd like to fetch, from this list ${allowedTypes.join(', ')}.\n e.g. --type=article` ); process.exit(1); } - const client = createPrismicClient(); - - const getGraphInfo = ({ - type, - isDetailed, - id, - }: { - type: (typeof allowedTypes)[number]; - isDetailed?: boolean; - id?: string; - }) => { - switch (type) { - case 'article': - return { - graphQueryName: isDetailed - ? 'articlesQuery' - : 'addressablesArticlesQuery', - id: id || 'ZdSMbREAACQA3j30', - }; - - case 'webcomic': - return { - graphQueryName: 'webcomicsQuery', - id: id || 'XkV9dREAAAPkNP0b', - }; - - case 'event': - return { - graphQueryName: isDetailed - ? 'eventDocumentsQuery' - : 'addressablesEventsQuery', - id: id || 'ZfhSyxgAACQAkLPZ', - }; - - case 'venue': - return { - graphQueryName: 'venueQuery', - id: id || 'Wsttgx8AAJeSNmJ4', - }; - - case 'exhibition': - return { - graphQueryName: 'addressablesExhibitionsQuery', - id: id || 'Yzv9ChEAABfUrkVp', - }; - - case 'book': - return { - graphQueryName: 'addressablesBooksQuery', - id: id || 'ZijgihEAACMAtL-k', - }; - - case 'page': - return { - graphQueryName: 'addressablesPagesQuery', - id: id || 'YdXSvhAAAIAW7YXQ', - }; - - case 'visual-story': - return { - graphQueryName: 'addressablesVisualStoriesQuery', - id: id || 'Zs8EuRAAAB4APxrA', - }; - - case 'exhibition-text': - return { - graphQueryName: 'addressablesExhibitionTextsQuery', - id: id || 'Zs8mohAAAB4AP4sc', - }; - - case 'exhibition-highlight-tour': - return { - graphQueryName: 'addressablesExhibitionHighlightToursQuery', - id: id || 'ZthrZRIAACQALvCC', - }; - - case 'project': - return { - graphQueryName: 'addressablesProjectsQuery', - id: id || 'Ys1-OxEAACEAguyS', - }; - - case 'season': - return { - graphQueryName: 'addressablesSeasonsQuery', - id: id || 'X84FvhIAACUAqiqp', - }; - - default: - console.error(`Allowed types are ${allowedTypes.join(', ')}.`); - process.exit(1); - } - }; - - const graphInfo = getGraphInfo({ type, isDetailed, id }); + const documentId = getDocumentId({ type, id }); + const graphQueryName = getGraphQueryName({ type, isDetailed }); const graphQuery = getGraphQuery({ type, isDetailed }); - if (!graphInfo || !graphQuery) { + if (!documentId || !graphQueryName || !graphQuery) { console.error('Something went wrong with queryResult', { - graphInfo, + documentId, + graphQueryName, hasGraphQuery: !!graphQuery, }); process.exit(1); } - const doc = await client.getByID(graphInfo.id, { + const client = createPrismicClient(); + + const doc = await client.getByID(documentId, { graphQuery: graphQuery.replace(/\n(\s+)/g, '\n'), }); console.log( util.inspect(doc, { showHidden: false, depth: null, colors: true }) ); - console.log( - `\n\x1b[4mThis is the result from ${graphInfo.graphQueryName}.\x1b[0m\n` - ); + console.log(`\n\x1b[4mThis is the result from ${graphQueryName}.\x1b[0m\n`); } main(); diff --git a/pipeline/src/scripts/testTransformer.ts b/pipeline/src/scripts/testTransformer.ts index 45dc8adf..77960c4a 100644 --- a/pipeline/src/scripts/testTransformer.ts +++ b/pipeline/src/scripts/testTransformer.ts @@ -2,22 +2,9 @@ import util from 'util'; import yargs from 'yargs'; import { - articlesQuery, - eventDocumentsQuery, - venueQuery, - webcomicsQuery, -} from '@weco/content-pipeline/src/graph-queries'; -import { - addressablesArticlesQuery, - addressablesBooksQuery, - addressablesEventsQuery, - addressablesExhibitionHighlightToursQuery, - addressablesExhibitionsQuery, - addressablesExhibitionTextsQuery, - addressablesPagesQuery, - addressablesVisualStoriesQuery, -} from '@weco/content-pipeline/src/graph-queries/addressables'; -import { getGraphQuery } from '@weco/content-pipeline/src/helpers/getGraphQuery'; + getGraphQuery, + isAddressablesAllowedTypes, +} from '@weco/content-pipeline/src/helpers/getGraphQuery'; import { createPrismicClient } from '@weco/content-pipeline/src/services/prismic'; import { transformAddressableArticle } from '@weco/content-pipeline/src/transformers/addressables/article'; import { transformAddressableBook } from '@weco/content-pipeline/src/transformers/addressables/book'; @@ -46,6 +33,8 @@ import { import { BookPrismicDocument } from '@weco/content-pipeline/src/types/prismic/books'; import { VenuePrismicDocument } from '@weco/content-pipeline/src/types/prismic/venues'; +import { getDocumentId } from './documentIds'; + const { type, isDetailed, id } = yargs(process.argv.slice(2)) .usage('Usage: $0 --type [string] --isDetailed [boolean] --id [string]') .options({ @@ -71,7 +60,7 @@ const allowedTypes = [ ]; async function main() { - if (!type) { + if (!isAddressablesAllowedTypes(type)) { console.error( `Please pass in the type you'd like to transform, from this list ${allowedTypes.join(', ')}.\n e.g. --type=article` ); @@ -82,149 +71,106 @@ async function main() { let transformerName; const transformDocument = async () => { + const documentId = getDocumentId({ type, id }); + const graphQuery = getGraphQuery({ type, isDetailed }).replace( + /\n(\s+)/g, + '\n' + ); + + if (!documentId || !graphQuery) { + console.error('Something went wrong with the request', { + documentId, + hasGraphQuery: !!graphQuery, + }); + process.exit(1); + } + + const doc = await client.getByID(documentId, { graphQuery }); + switch (type) { case 'article': { - const doc = await client.getByID(id || 'ZdSMbREAACQA3j30', { - graphQuery: `{ - ${isDetailed ? articlesQuery : addressablesArticlesQuery} - }`.replace(/\n(\s+)/g, '\n'), - }); - transformerName = isDetailed ? 'transformArticle' : 'transformAddressableArticle'; + return isDetailed ? transformArticle(doc as ArticlePrismicDocument) : transformAddressableArticle(doc as ArticlePrismicDocument); } case 'webcomic': { - const doc = await client.getByID(id || 'XkV9dREAAAPkNP0b', { - graphQuery: `{ - ${webcomicsQuery} - }`.replace(/\n(\s+)/g, '\n'), - }); - transformerName = 'transformArticle'; + return transformArticle(doc as ArticlePrismicDocument); } case 'event': { - const doc = await client.getByID(id || 'ZfhSyxgAACQAkLPZ', { - graphQuery: (isDetailed - ? eventDocumentsQuery - : `{ - ${addressablesEventsQuery} - }` - ).replace(/\n(\s+)/g, '\n'), - }); - transformerName = isDetailed ? 'transformEventDocument' : 'transformAddressableEvent'; + return isDetailed ? transformEventDocument(doc as EventPrismicDocument) : transformAddressableEvent(doc as EventPrismicDocument); } case 'venue': { - const doc = await client.getByID(id || 'Wsttgx8AAJeSNmJ4', { - graphQuery: venueQuery.replace(/\n(\s+)/g, '\n'), - }); - transformerName = 'transformVenue'; + return transformVenue(doc as VenuePrismicDocument); } case 'exhibition': { - const doc = await client.getByID(id || 'Yzv9ChEAABfUrkVp', { - graphQuery: `{ - ${addressablesExhibitionsQuery.replace(/\n(\s+)/g, '\n')} - }`, - }); - transformerName = 'transformAddressableExhibition'; + return transformAddressableExhibition(doc as ExhibitionPrismicDocument); } case 'book': { - const doc = await client.getByID(id || 'WwVK3CAAAHm5Exxr', { - graphQuery: `{ - ${addressablesBooksQuery.replace(/\n(\s+)/g, '\n')} - }`, - }); - transformerName = 'transformAddressableBook'; + return transformAddressableBook(doc as BookPrismicDocument); } case 'page': { - const doc = await client.getByID(id || 'YdXSvhAAAIAW7YXQ', { - graphQuery: `{ - ${addressablesPagesQuery.replace(/\n(\s+)/g, '\n')} - }`, - }); - transformerName = 'transformAddressablePage'; + return transformAddressablePage(doc as PagePrismicDocument); } case 'visual-story': { - const doc = await client.getByID(id || 'Zs8EuRAAAB4APxrA', { - graphQuery: `{ - ${addressablesVisualStoriesQuery.replace(/\n(\s+)/g, '\n')} - }`, - }); - transformerName = 'transformAddressableVisualStory'; + return transformAddressableVisualStory( doc as VisualStoryPrismicDocument ); } case 'exhibition-text': { - const doc = await client.getByID(id || 'Zs8mohAAAB4AP4sc', { - graphQuery: `{ - ${addressablesExhibitionTextsQuery.replace(/\n(\s+)/g, '\n')} - }`, - }); - transformerName = 'transformAddressableExhibitionText'; + return transformAddressableExhibitionText( doc as ExhibitionTextPrismicDocument ); } case 'exhibition-highlight-tour': { - const doc = await client.getByID(id || 'ZthrZRIAACQALvCC', { - graphQuery: `{ - ${addressablesExhibitionHighlightToursQuery.replace(/\n(\s+)/g, '\n')} - }`, - }); - transformerName = 'transformAddressableExhibitionHighlightTour'; + return transformAddressableExhibitionHighlightTour( doc as ExhibitionHighlightTourPrismicDocument ); } case 'project': { - const graphQuery = getGraphQuery({ type: 'project' }); - const doc = await client.getByID(id || 'YLokOhAAACQAf8Hd', { - graphQuery: graphQuery.replace(/\n(\s+)/g, '\n'), - }); - transformerName = 'transformProject'; + return transformAddressableProject(doc as ProjectPrismicDocument); } case 'season': { - const graphQuery = getGraphQuery({ type: 'season' }); - const doc = await client.getByID(id || 'X84FvhIAACUAqiqp', { - graphQuery: graphQuery.replace(/\n(\s+)/g, '\n'), - }); - transformerName = 'transformSeason'; + return transformAddressableSeason(doc as SeasonPrismicDocument); } diff --git a/pipeline/test/update-prismic-snapshots.ts b/pipeline/test/update-prismic-snapshots.ts index dce90cc1..346d6426 100644 --- a/pipeline/test/update-prismic-snapshots.ts +++ b/pipeline/test/update-prismic-snapshots.ts @@ -11,7 +11,11 @@ import { webcomicsQuery, wrapQueries, } from '@weco/content-pipeline/src/graph-queries'; -import { getGraphQuery } from '@weco/content-pipeline/src/helpers/getGraphQuery'; +import { + allowedTypes, + getGraphQuery, + isAddressablesAllowedTypes, +} from '@weco/content-pipeline/src/helpers/getGraphQuery'; import { asText, asTitle, @@ -124,6 +128,12 @@ const updateAddressablesSnapshots = async (client: Client) => { const docs = await Promise.all( Object.entries(addressableIds).map(async ([key, value]) => { + if (!isAddressablesAllowedTypes(key)) { + console.error( + `Please pass in the type you'd like to fetch, from this list ${allowedTypes.join(', ')}.\n e.g. --type=article` + ); + process.exit(1); + } const graphQuery = getGraphQuery({ type: key }); const newDoc = await client.getByID(value, {