diff --git a/src/lib/e-lattice.test.ts b/src/lib/e-lattice.test.ts new file mode 100644 index 0000000..f9cee01 --- /dev/null +++ b/src/lib/e-lattice.test.ts @@ -0,0 +1,16 @@ +// @ts-expect-error Runtime types come from `bun test`, not the app tsconfig. +import { expect, test } from 'bun:test' +import { inSight, SIGHT_LIMIT } from './e-lattice.ts' + +test('a lattice point is in sight only when nothing else sits on the ray', () => { + expect(inSight(3, 4)).toBe(true) + expect(inSight(-3, 4)).toBe(true) + expect(inSight(1, 0)).toBe(true) + expect(inSight(2, 4)).toBe(false) + expect(inSight(0, 2)).toBe(false) + expect(inSight(0, 0)).toBe(false) +}) + +test('the visible-share limit is 6 over pi squared', () => { + expect(SIGHT_LIMIT).toBe(6 / Math.PI ** 2) +}) diff --git a/src/lib/token-classes.test.ts b/src/lib/token-classes.test.ts new file mode 100644 index 0000000..3ff5034 --- /dev/null +++ b/src/lib/token-classes.test.ts @@ -0,0 +1,31 @@ +// @ts-expect-error Runtime types come from `bun test`, not the app tsconfig. +import { expect, test } from 'bun:test' +import { codeToHtml } from 'shiki' +import { createCssVariablesTheme } from 'shiki/core' +import { tokenClasses } from './token-classes.ts' + +// Astro's markdown highlighter, not the bundled theme named `css-variables`. +const theme = createCssVariablesTheme({ variablePrefix: '--astro-code-' }) + +const highlight = (code: string, lang: string): Promise => + codeToHtml(code, { lang, theme, transformers: [tokenClasses] }) + +test('highlighted tokens keep colour as tok-* classes instead of inline style', async () => { + const html = await highlight('const x = "hi"', 'javascript') + expect(html).not.toContain('style=') + expect(html).toContain('class="tok-keyword"') + expect(html).toContain('class="tok-constant"') + expect(html).toContain('class="tok-string-expression"') +}) + +test('a shell flag that Shiki files as a string is marked tok-flag', async () => { + const html = await highlight('echo -n hi', 'bash') + expect(html).toContain('class="tok-flag"> -n') + expect(html).toContain('class="tok-string"> hi') +}) + +test('a span whose colour is not a token variable gets no tok class', async () => { + const html = await highlight('echo \\\n -v', 'bash') + expect(html).toContain(' \\') + expect(html).toContain('class="tok-flag"> -v') +})