Skip to content

Commit

Permalink
ninth work session; start creating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trueberryless committed Jan 8, 2025
1 parent 018d343 commit d564605
Show file tree
Hide file tree
Showing 17 changed files with 647 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/starlight-spell-checker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"devDependencies": {
"@astrojs/starlight": "^0.30.3",
"astro": "^5.1.2"
"astro": "^5.1.2",
"vitest": "^2.1.8"
},
"peerDependencies": {
"@astrojs/starlight": ">=0.30"
Expand Down Expand Up @@ -76,5 +77,8 @@
"unified": "^11.0.5",
"unist-util-visit": "^5.0.0",
"vfile-reporter": "^8.1.1"
},
"scripts": {
"test": "vitest"
}
}
107 changes: 107 additions & 0 deletions packages/starlight-spell-checker/tests/basics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { expect, test } from 'vitest'

import { ValidationErrorType } from '../libs/validation'

import { buildFixture, expectValidationErrorCount, expectValidationErrors } from './utils'

test('builds with valid English content', async () => {
const { status } = await buildFixture('valid-content')

expect(status).toBe('success')
})

test('builds with valid German content', async () => {
const { status } = await buildFixture('valid-content-de')

expect(status).toBe('success')
})

test('builds with valid French content', async () => {
const { status } = await buildFixture('valid-content-fr')

expect(status).toBe('success')
})

// test('does not build with invalid links', async () => {
// const { output, status } = await buildFixture('invalid-links')

// expect(status).toBe('error')

// expectValidationErrorCount(output, 64, 4)

// expectValidationErrors(output, 'test/', [
// ['/https://starlight.astro.build/', ValidationErrorType.InvalidLink],
// ['/', ValidationErrorType.InvalidLink],
// ['/unknown', ValidationErrorType.InvalidLink],
// ['/unknown/', ValidationErrorType.InvalidLink],
// ['/unknown#title', ValidationErrorType.InvalidLink],
// ['/unknown/#title', ValidationErrorType.InvalidLink],
// ['/draft', ValidationErrorType.InvalidLink],
// ['/draft/', ValidationErrorType.InvalidLink],
// ['#links', ValidationErrorType.InvalidHash],
// ['/guides/example/#links', ValidationErrorType.InvalidHash],
// ['/icon.svg', ValidationErrorType.InvalidLink],
// ['/guidelines/ui.pdf', ValidationErrorType.InvalidLink],
// ['/unknown-ref', ValidationErrorType.InvalidLink],
// ['#unknown-ref', ValidationErrorType.InvalidHash],
// ['#anotherDiv', ValidationErrorType.InvalidHash],
// ['/guides/page-with-custom-slug', ValidationErrorType.InvalidLink],
// ['/release/@pkg/v0.2.0', ValidationErrorType.InvalidLink],
// ['/?query=string', ValidationErrorType.InvalidLink],
// ['/unknown?query=string', ValidationErrorType.InvalidLink],
// ['/unknown/?query=string', ValidationErrorType.InvalidLink],
// ['/unknown?query=string#title', ValidationErrorType.InvalidLink],
// ['/unknown/?query=string#title', ValidationErrorType.InvalidLink],
// ['?query=string#links', ValidationErrorType.InvalidHash],
// ['/guides/example/?query=string#links', ValidationErrorType.InvalidHash],
// ['/icon.svg?query=string', ValidationErrorType.InvalidLink],
// ['/guidelines/ui.pdf?query=string', ValidationErrorType.InvalidLink],
// ['/unknown-ref?query=string', ValidationErrorType.InvalidLink],
// ['?query=string#unknown-ref', ValidationErrorType.InvalidHash],
// ['http://localhost', ValidationErrorType.LocalLink],
// ['http://localhost:4321/', ValidationErrorType.LocalLink],
// ['https://127.0.0.1:4321/getting-started', ValidationErrorType.LocalLink],
// ])

// expectValidationErrors(output, 'guides/example/', [
// ['#links', ValidationErrorType.InvalidHash],
// ['/unknown/#links', ValidationErrorType.InvalidLink],
// ['/unknown', ValidationErrorType.InvalidLink],
// ['#anotherBlock', ValidationErrorType.InvalidHash],
// ['/icon.svg', ValidationErrorType.InvalidLink],
// ['/guidelines/ui.pdf', ValidationErrorType.InvalidLink],
// ['/linkcard/', ValidationErrorType.InvalidLink],
// ['/linkcard/#links', ValidationErrorType.InvalidLink],
// ['#linkcard', ValidationErrorType.InvalidHash],
// ['/linkbutton/', ValidationErrorType.InvalidLink],
// ['/linkbutton/#links', ValidationErrorType.InvalidLink],
// ['#linkbutton', ValidationErrorType.InvalidHash],
// ['?query=string#links', ValidationErrorType.InvalidHash],
// ['/unknown/?query=string#links', ValidationErrorType.InvalidLink],
// ['/unknown?query=string', ValidationErrorType.InvalidLink],
// ['/icon.svg?query=string', ValidationErrorType.InvalidLink],
// ['/guidelines/ui.pdf?query=string', ValidationErrorType.InvalidLink],
// ['/linkcard/?query=string', ValidationErrorType.InvalidLink],
// ['/linkbutton/?query=string', ValidationErrorType.InvalidLink],
// ])

// expectValidationErrors(output, 'guides/namespacetest/', [
// ['#some-other-content', ValidationErrorType.InvalidHash],
// ['/guides/namespacetest/#another-content', ValidationErrorType.InvalidHash],
// ])

// expectValidationErrors(output, 'relative/', [
// ['.', ValidationErrorType.RelativeLink],
// ['./relative', ValidationErrorType.RelativeLink],
// ['./test', ValidationErrorType.RelativeLink],
// ['./guides/example', ValidationErrorType.RelativeLink],
// ['../test', ValidationErrorType.RelativeLink],
// ['test', ValidationErrorType.RelativeLink],
// ['.?query=string', ValidationErrorType.RelativeLink],
// ['./relative?query=string', ValidationErrorType.RelativeLink],
// ['./test?query=string', ValidationErrorType.RelativeLink],
// ['./guides/example?query=string', ValidationErrorType.RelativeLink],
// ['../test?query=string', ValidationErrorType.RelativeLink],
// ['test?query=string', ValidationErrorType.RelativeLink],
// ])
// })
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import starlight from "@astrojs/starlight";
import { defineConfig } from "astro/config";
import starlightSpellChecker from "starlight-spell-checker";

export default defineConfig({
integrations: [
starlight({
pagefind: false,
plugins: [starlightSpellChecker()],
title: "Starlight Spell Checker Tests - valid content de",
}),
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@starlight-spell-checker-tests/valid-content-de",
"version": "0.0.0",
"dependencies": {
"@astrojs/starlight": "^0.30.2",
"astro": "^5.1.1",
"starlight-spell-checker": "workspace:*"
},
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { docsLoader } from '@astrojs/starlight/loaders'
import { docsSchema } from '@astrojs/starlight/schema'
import { defineCollection } from 'astro:content'

export const collections = {
docs: defineCollection({ loader: docsLoader(), schema: docsSchema() }),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Verzeichnis
---

Die Sonne tauchte unter den Horizont und malte den Himmel in orangefarbenen und rosafarbenen Tönen, während die Abendbrise durch die Bäume rauschte.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import starlight from "@astrojs/starlight";
import { defineConfig } from "astro/config";
import starlightSpellChecker from "starlight-spell-checker";

export default defineConfig({
integrations: [
starlight({
pagefind: false,
plugins: [starlightSpellChecker()],
title: "Starlight Spell Checker Tests - valid content fr",
}),
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@starlight-spell-checker-tests/valid-content-fr",
"version": "0.0.0",
"dependencies": {
"@astrojs/starlight": "^0.30.2",
"astro": "^5.1.1",
"starlight-spell-checker": "workspace:*"
},
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { docsLoader } from '@astrojs/starlight/loaders'
import { docsSchema } from '@astrojs/starlight/schema'
import { defineCollection } from 'astro:content'

export const collections = {
docs: defineCollection({ loader: docsLoader(), schema: docsSchema() }),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Indice
---

Le soleil a plongé sous l'horizon, peignant le ciel dans des teintes orange et roses tandis que la brise du soir murmurait à travers les arbres.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import starlight from "@astrojs/starlight";
import { defineConfig } from "astro/config";
import starlightSpellChecker from "starlight-spell-checker";

export default defineConfig({
integrations: [
starlight({
pagefind: false,
plugins: [starlightSpellChecker()],
title: "Starlight Spell Checker Tests - valid content",
}),
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@starlight-spell-checker-tests/valid-content",
"version": "0.0.0",
"dependencies": {
"@astrojs/starlight": "^0.30.2",
"astro": "^5.1.1",
"starlight-spell-checker": "workspace:*"
},
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { docsLoader } from '@astrojs/starlight/loaders'
import { docsSchema } from '@astrojs/starlight/schema'
import { defineCollection } from 'astro:content'

export const collections = {
docs: defineCollection({ loader: docsLoader(), schema: docsSchema() }),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Index
---

The sun dipped below the horizon, painting the sky in hues of orange and pink as the evening breeze whispered through the trees.
124 changes: 124 additions & 0 deletions packages/starlight-spell-checker/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { fileURLToPath } from "node:url";

import { build } from "astro";
import { expect, vi } from "vitest";

import type { ValidationErrorType } from "../libs/validation";

export async function buildFixture(name: string) {
const fixturePath = fileURLToPath(
new URL(`fixtures/${name}/`, import.meta.url)
);

let output = "";
let status: "success" | "error";

function writeOutput(chunk: string | Uint8Array) {
output += String(chunk);
return true;
}

const stdoutWriteSpy = vi
.spyOn(process.stdout, "write")
.mockImplementation(writeOutput);
const stderrWriteSpy = vi
.spyOn(process.stderr, "write")
.mockImplementation(writeOutput);

try {
await build({ root: fixturePath });
status = "success";
} catch {
status = "error";
}

stderrWriteSpy.mockRestore();
stdoutWriteSpy.mockRestore();

return { output, status };
}

export function expectValidationWarningCount(
output: string,
count: number,
filesCount: number
) {
expect(output).toMatch(
new RegExp(
`Found ${count} ${
count === 1 ? "warning" : "warnings"
} in ${filesCount} ${filesCount === 1 ? "file" : "files"}.`
)
);
}

export function expectValidationWarnings(
output: string,
path: string,
validationWarnings: [
word: string,
type: ValidationErrorType,
suggestions?: string[]
][]
) {
expect(output).toMatch(
new RegExp(`▶ ${path}
${validationWarnings
.map(
([word, type, suggestions], index) =>
`.* ${
index < validationWarnings.length - 1 ? "├" : "└"
}${word.replaceAll("?", String.raw`\?`)} - ${type}${
suggestions
? suggestions.length > 0
? ` \(${suggestions.join(", ")}\)`
: " no suggestions"
: ""
}`
)
.join("\n")}`)
);
}

export function expectValidationErrorCount(
output: string,
count: number,
filesCount: number
) {
expect(output).toMatch(
new RegExp(
`Found ${count} ${count === 1 ? "error" : "errors"} in ${filesCount} ${
filesCount === 1 ? "file" : "files"
}.`
)
);
}

export function expectValidationErrors(
output: string,
path: string,
validationErrors: [
word: string,
type: ValidationErrorType,
suggestions?: string[]
][]
) {
expect(output).toMatch(
new RegExp(`▶ ${path}
${validationErrors
.map(
([word, type, suggestions], index) =>
`.* ${index < validationErrors.length - 1 ? "├" : "└"}${word.replaceAll(
"?",
String.raw`\?`
)} - ${type}${
suggestions
? suggestions.length > 0
? ` \(${suggestions.join(", ")}\)`
: " no suggestions"
: ""
}`
)
.join("\n")}`)
);
}
13 changes: 13 additions & 0 deletions packages/starlight-spell-checker/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
poolOptions: {
forks: {
maxForks: 1,
minForks: 1,
},
},
testTimeout: 60_000,
},
});
Loading

0 comments on commit d564605

Please sign in to comment.