diff --git a/apps/website/scripts/check-models-html.ts b/apps/website/scripts/check-models-html.ts index 500afc8fde4..5d1a2e4012e 100644 --- a/apps/website/scripts/check-models-html.ts +++ b/apps/website/scripts/check-models-html.ts @@ -4,27 +4,32 @@ import { join } from 'node:path' import { workshopModels } from '@/config/workshop-browse-content' import { getRouterWorkshopModelDetail } from '@/config/workshop-router-content' import { auditExampleGallery } from './models-gallery-audit' -import { auditMediaLabels, auditModelPage } from './models-html-audit' +import { + auditMediaLabels, + auditModelDefinition, + auditModelPage +} from './models-html-audit' const DIST = join(process.cwd(), 'dist') -function examplesOf(slug: string) { +function detailOf(slug: string) { const detail = getRouterWorkshopModelDetail(slug) if (!detail) throw new Error(`Missing model record: ${slug}`) - return detail.examples + return detail } -const pagedModels = workshopModels.flatMap(({ href, name, slug }) => - href === undefined ? [] : [{ href, name, slug }] +const pagedModels = workshopModels.flatMap(({ href, slug }) => + href === undefined ? [] : [{ href, detail: detailOf(slug) }] ) -const errors = pagedModels.flatMap((model) => { - const html = readFileSync(join(DIST, model.href, 'index.html'), 'utf-8') +const errors = pagedModels.flatMap(({ href, detail }) => { + const html = readFileSync(join(DIST, href, 'index.html'), 'utf-8') return [ - ...auditModelPage(html, model.name), + ...auditModelPage(html, detail.name), + ...auditModelDefinition(html, detail), ...auditMediaLabels(html), - ...auditExampleGallery(html, examplesOf(model.slug).length) - ].map((error) => `${model.href}: ${error}`) + ...auditExampleGallery(html, detail.examples.length) + ].map((error) => `${href}: ${error}`) }) if (errors.length > 0) { diff --git a/apps/website/scripts/models-html-audit.test.ts b/apps/website/scripts/models-html-audit.test.ts index 091ddbaa43a..f5500b6d814 100644 --- a/apps/website/scripts/models-html-audit.test.ts +++ b/apps/website/scripts/models-html-audit.test.ts @@ -1,6 +1,12 @@ import { describe, expect, it } from 'vitest' -import { auditMediaLabels, auditModelPage } from './models-html-audit' +import type { RouterWorkshopModel } from '@/config/models-catalogue' + +import { + auditMediaLabels, + auditModelDefinition, + auditModelPage +} from './models-html-audit' const showcase = '

Grok Imagine in ComfyUI

' const related = (cards: string) => @@ -51,6 +57,84 @@ describe(auditModelPage, () => { }) }) +describe(auditModelDefinition, () => { + const model: RouterWorkshopModel = { + slug: 'bfl--flux-pro-fill--edit-images', + name: 'FLUX Pro Fill', + provider: 'Black Forest Labs', + task: 'image-to-image', + modality: 'image', + workflowCount: 0, + capabilities: [], + routerId: 'bfl/flux-pro-fill' + } + const sentence = + 'FLUX Pro Fill is an image-to-image model from Black Forest Labs. You can call it through the Comfy Router API as bfl/flux-pro-fill.' + const definition = (text: string) => + `

\n ${text}\n

` + const facts = (rows: string) => + `
${rows}
` + const routerRow = (routerId: string) => + `
Router model ID
${routerId}
` + const wrongDefinition = (found: string) => + `expected the definition ${JSON.stringify(sentence)}, found ${JSON.stringify(found)}` + const missingRouterRow = + 'expected a "Router model ID" row reading bfl/flux-pro-fill' + + it.for([ + { + name: 'the sentence and the Router id row', + html: + definition(sentence) + + facts(`
Provider
BFL
${routerRow(model.routerId)}`), + errors: [] + }, + { + name: 'only the Router clause', + html: + definition( + 'You can call it through the Comfy Router API as bfl/flux-pro-fill.' + ) + facts(routerRow(model.routerId)), + errors: [ + wrongDefinition( + 'You can call it through the Comfy Router API as bfl/flux-pro-fill.' + ) + ] + }, + { + name: 'a tampered first sentence', + html: + definition(sentence.replace('an image', 'a image')) + + facts(routerRow(model.routerId)), + errors: [wrongDefinition(sentence.replace('an image', 'a image'))] + }, + { + name: 'an empty sentence and no facts', + html: definition('') + facts(''), + errors: [wrongDefinition(''), missingRouterRow] + }, + { + name: 'the sentence only inside a template', + html: ``, + errors: [wrongDefinition(''), missingRouterRow] + }, + { + name: "another model's Router id", + html: + definition(sentence.replace('bfl/flux-pro-fill', 'bfl/flux-2-pro')) + + facts(routerRow('bfl/flux-2-pro')), + errors: [ + wrongDefinition( + sentence.replace('bfl/flux-pro-fill', 'bfl/flux-2-pro') + ), + missingRouterRow + ] + } + ])('$name', ({ html, errors }) => { + expect(auditModelDefinition(html, model)).toEqual(errors) + }) +}) + describe(auditMediaLabels, () => { it.for([ { diff --git a/apps/website/scripts/models-html-audit.ts b/apps/website/scripts/models-html-audit.ts index ceee30dbfa5..de662f416bb 100644 --- a/apps/website/scripts/models-html-audit.ts +++ b/apps/website/scripts/models-html-audit.ts @@ -1,3 +1,6 @@ +import type { RouterWorkshopModel } from '@/config/models-catalogue' +import { modelDefinition } from '@/lib/workshop/model-definition' + const ENTITIES: Record = { '&': '&', '<': '<', @@ -39,6 +42,35 @@ export function auditModelPage(html: string, modelName: string): string[] { return errors } +function innerOf(html: string, tag: string, testId: string) { + return new RegExp( + `<${tag}\\b[^>]*data-testid="${testId}"[^>]*>([\\s\\S]*?)` + ).exec(html)?.[1] +} + +export function auditModelDefinition( + html: string, + model: RouterWorkshopModel +): string[] { + const { routerId } = model + const live = withoutHiddenMarkup(html) + const errors: string[] = [] + const definition = textOf(innerOf(live, 'p', 'model-definition') ?? '') + const expected = modelDefinition(model) + if (definition !== expected) + errors.push( + `expected the definition ${JSON.stringify(expected)}, found ${JSON.stringify(definition)}` + ) + const facts = innerOf(live, 'dl', 'model-facts') ?? '' + const routerRow = + /]*>\s*Router model ID\s*<\/dt>\s*]*>([\s\S]*?)<\/dd>/.exec( + facts + )?.[1] + if (routerRow === undefined || textOf(routerRow) !== routerId) + errors.push(`expected a "Router model ID" row reading ${routerId}`) + return errors +} + const PLACEHOLDER_LABEL = /^(?:Output|Sample \d+)$/ function attributeOf(tag: string, name: string): string | undefined { diff --git a/apps/website/src/components/workshop/ModelPage.vue b/apps/website/src/components/workshop/ModelPage.vue index 6faea4e3b80..bede2b642dd 100644 --- a/apps/website/src/components/workshop/ModelPage.vue +++ b/apps/website/src/components/workshop/ModelPage.vue @@ -1,5 +1,6 @@