Skip to content
Myr

pierre.test.ts

This file

Path

apps/web/test/pierre.test.ts

114 lines · 3.4 KiB · TypeScript

Last changed by

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

History of this file

Take it

Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e

lines 1–11why, one step back
1import { describe, expect, it, vi } from "@effect/vitest";
2import { DEFAULT_TOKENIZE_MAX_LENGTH, resolveTheme } from "@pierre/diffs";3import { Effect } from "effect";45import {6 CODE_THEME,7 diffsOptions,8 registerCodeTheme,9 treesOptions,10} from "../src/features/code/pierre.ts";11

Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5

lines 12–19why, one step back
12const box = (scrolls: boolean) => ({
13 clientHeight: 400,14 clientWidth: 600,15 scrollHeight: 400,16 scrollWidth: scrolls ? 900 : 600,17 tabIndex: -1,18});19

Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e

lines 20–34why, one step back
20describe("the options every drawing of code shares", () => {
21 it("name the token theme for both schemes, so the page's scheme decides", () => {22 expect(diffsOptions.theme).toStrictEqual({23 dark: CODE_THEME,24 light: CODE_THEME,25 });26 expect(diffsOptions.themeType).toBe("system");27 expect(diffsOptions.preferredHighlighter).toBe("shiki-js");28 });2930 it("carry the library's own limits, written out", () => {31 expect(diffsOptions.tokenizeMaxLength).toBe(100_000);32 expect(diffsOptions.tokenizeMaxLength).toBe(DEFAULT_TOKENIZE_MAX_LENGTH);33 expect(diffsOptions.tokenizeMaxLineLength).toBe(1000);34 expect(diffsOptions.maxLineDiffLength).toBe(1000);

Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5

lines 35–46why, one step back
35 });
3637 it("let the keyboard reach code that scrolls, with its focus in sight", () => {38 const wide = box(true);39 const narrow = box(false);4041 diffsOptions.onPostRender({42 shadowRoot: { querySelectorAll: () => [wide, narrow] },43 });4445 expect([wide.tabIndex, narrow.tabIndex]).toStrictEqual([0, -1]);46 expect(diffsOptions.unsafeCSS).toContain("code:focus-visible");

Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e

lines 47–114why, one step back
47 });
4849 it("draw a file tree compact, with plain icons", () => {50 expect(treesOptions).toStrictEqual({51 density: "compact",52 icons: "minimal",53 });54 });55});5657describe("the token theme", () => {58 it.effect("colours code from the web's variables", () =>59 Effect.gen(function* () {60 registerCodeTheme();6162 const theme = yield* Effect.promise(() => resolveTheme(CODE_THEME));63 const colours = JSON.stringify(theme);6465 for (const token of ["keyword", "string", "comment", "constant"]) {66 expect(colours).toContain(`var(--diffs-token-${token}`);67 }6869 expect(colours).toContain("var(--diffs-background");70 })71 );7273 it("is registered once, however many drawings ask for it", () => {74 const complaints = vi.spyOn(console, "error").mockImplementation(() => {75 // Pierre says so on the console when a theme name is taken twice.76 });7778 registerCodeTheme();79 registerCodeTheme();8081 expect(complaints).not.toHaveBeenCalled();82 complaints.mockRestore();83 });84});8586describe("the drawings of code", () => {87 const sources: Readonly<Record<string, string>> = import.meta.glob(88 "../src/**/*.tsx",89 { eager: true, import: "default", query: "?raw" }90 );9192 const drawing = /from\s+["']@pierre\/(?:diffs|trees)\/react["']/u;93 const shared = /from\s+["'](?:\.\.?\/)+(?:code\/)?pierre\.ts["']/u;9495 const drawings = Object.entries(sources).filter(([, source]) =>96 drawing.test(source)97 );9899 it("each take their options from the one module", () => {100 expect(drawings.length).toBeGreaterThanOrEqual(6);101102 expect(103 drawings.flatMap(([path, source]) => (shared.test(source) ? [] : [path]))104 ).toStrictEqual([]);105 });106107 it("never fall back to Pierre's own colours", () => {108 expect(109 Object.entries(sources).flatMap(([path, source]) =>110 source.includes("DEFAULT_THEMES") ? [path] : []111 )112 ).toStrictEqual([]);113 });114});