Skip to content

Commit

Permalink
chore: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
holloway committed Feb 3, 2025
1 parent 24ed2dc commit b121ef0
Showing 1 changed file with 109 additions and 7 deletions.
116 changes: 109 additions & 7 deletions client/utilities/rfc-index.html.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { vi, describe, beforeEach, afterEach, it, expect } from 'vitest'
import fs from 'node:fs'
import { XMLParser } from 'fast-xml-parser'
import { XMLBuilder, XMLParser } from 'fast-xml-parser'
import { rfcToRfcSummary } from './rfc-index-html'
import { getAllRFCs } from './redClientWrappers'
import { PRIVATE_API_URL } from './url'
import { PRIVATE_API_URL, rfcPathBuilder } from './url'
import {
blankRfcResponse,
twoDigitOldestRfcResponse,
Expand All @@ -15,6 +15,9 @@ import {
type PaginatedRfcMetadataList
} from '~/generated/red-client'
import path from 'node:path'
import {} from 'vue'
import { renderToString } from 'vue/server-renderer'
import { parseRFCId } from './rfc'

const originalRfcIndexHtml = fs
.readFileSync(path.join(import.meta.dirname, 'rfc-index.html'), 'utf-8')
Expand All @@ -34,10 +37,95 @@ const parseHtml = (html: string) => {

const originalRfcIndex = parseHtml(originalRfcIndexHtml)

console.log(originalRfcIndex)
type XMLBuilderOptions = NonNullable<
ConstructorParameters<typeof XMLBuilder>[0]
>

const extractRfcSummaries = (html: unknown) => {
return []
const xmlBuilderOptions: XMLBuilderOptions = {
preserveOrder: true,
ignoreAttributes: true,
attributeNamePrefix: '',
format: true,
suppressBooleanAttributes: true,
indentBy: ' '
}

const extractRfcSummaries = (document: unknown) => {
if (!Array.isArray(document)) {
console.log('Bad param document', document)
throw Error('Bad param document')
}
const root = document[0]['html']
const body = root[1]['body']
const tables = body.filter((row: { table?: any }) => Boolean(row.table))
const rfcsTable = tables[2]
const rows = rfcsTable.table
if (!Array.isArray(rows)) {
console.log('Bad param rows', rows)
throw Error('Bad param rows')
}
const builder = new XMLBuilder(xmlBuilderOptions)
const htmlRows = rows.map((row: any) => {
/** Don't use regex for HTML...unless it's a very specific structure which this is
(ie, we're not dealing with arbitrary HTML so this is safe)
*/

const numCell = row.tr[0]
const numWithScripts: string = builder.build(numCell.td)

const num = numWithScripts
.replace(
/<script[\s\S]*?<\/script>/gi,
// we don't need any scripts because in our HTML there's always a companion <noscript>
''
)
.replace(/<noscript>(.*?)<\/noscript>/gi, (_match, rfcNumber) => {
/**
The HTML looks like this:
<script> doStandardDocLink(&apos;RFC9280&apos;); </script>
<noscript>9280</noscript>
which is converted to:
<a href="/info/rfc9280/">RFC9280</a>
*/
return `<a href="${rfcPathBuilder(`RFC${rfcNumber}`)}">${parseInt(rfcNumber, 10)}</a>`
})
.replace(/[\s]+/g, ' ') // normalise the whitespace
.trim()

const informationCell = row.tr[1]
const informationWithScripts: string = builder.build(informationCell.td)
const information = informationWithScripts
.replace(
/<script[\s\S]*?<\/script>/gi,
// we don't need any scripts because in our HTML there's always a companion <noscript>
''
)
.replace(/<noscript>(.*?)<\/noscript>/gi, (_match, rfcId) => {
/**
The HTML looks like this:
<script> doStandardDocLink(&apos;RFC9280&apos;); </script>
<noscript>RFC9280</noscript>
which is converted to:
<a href="/info/rfc9280/">RFC9280</a>
*/
return `<a href="${rfcPathBuilder(rfcId)}">RFC${parseRFCId(rfcId).number}</a>`
})
.replace(/[\s]+/g, ' ') // normalise the whitespace
.trim()

return [num, information]
})

// Remove the first row of the table because it's headings
htmlRows.shift()

return htmlRows
}

const originalRfcSummaries = extractRfcSummaries(originalRfcIndex)
Expand Down Expand Up @@ -88,10 +176,24 @@ describe('renderRfcIndexDotTxt', () => {
seekingResponses: [twoDigitRFCDocListResponse]
})
const rfcs = await getAllRFCs(apiClient)
const rfcSummaries = rfcs.map(rfcToRfcSummary)
const rfcSummariesAsVNodes = rfcs.map(rfcToRfcSummary)
const rfcSummariesWithExtraHTML = await Promise.all(
rfcSummariesAsVNodes.map((rfcSummaryAsVNode) =>
// Returns HTML that looks like
// <span><!--[-->S. Crocker<!--]--> [ April 1969 ] (TXT, HTML)<!--[--><!--]--> (Status: unknown) (Stream: Legacy)(doi: 10.17487/RFC0001)</span>"
renderToString(rfcSummaryAsVNode.body, {})
)
)
const rfcSummaries = rfcSummariesWithExtraHTML.map(
(rfcSummaryWithExtraHTML) =>
rfcSummaryWithExtraHTML
.replace(/<!--[\s\S]*?-->/g, '')
.replace(/^<span>/i, '')
.replace(/<\/span>$/i, '')
)

rfcSummaries.forEach((rfcSummary, index) => {
const originalRfcSummary = originalRfcSummaries[index]
const originalRfcSummary = originalRfcSummaries[index][1]
expect(rfcSummary).toBe(originalRfcSummary)

Check failure on line 197 in client/utilities/rfc-index.html.test.ts

View workflow job for this annotation

GitHub Actions / build

utilities/rfc-index.html.test.ts > renderRfcIndexDotTxt > original rfc-index.html rendering

AssertionError: expected 'S. Crocker [ April 1969 ] (TXT, HTML)…' to be '<b>Host Software</b> S. Crocker [ Apr…' // Object.is equality Expected: "<b>Host Software</b> S. Crocker [ April 1969 ] (TXT, HTML) (Status: UNKNOWN) (Stream: Legacy) (DOI: 10.17487/RFC0001)" Received: "S. Crocker [ April 1969 ] (TXT, HTML) (Status: unknown) (Stream: Legacy)(doi: 10.17487/RFC0001)" ❯ utilities/rfc-index.html.test.ts:197:26 ❯ utilities/rfc-index.html.test.ts:195:18
})
})
Expand Down

0 comments on commit b121ef0

Please sign in to comment.