Skip to content

The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.

3.7 KB
Raw Blame
// The link standard (docs/design-principles.md, rule 18), checked over the web app's source: a place in the
// app named inside text is an AppLink (ui/AppLink.tsx), an action inside text a LinkButton, and nothing else builds
// the link look by hand, so there is one style and one way to open a place.
import { readdirSync, readFileSync } from 'node:fs'
import { join, relative } from 'node:path'
import { expect, test } from 'vitest'

const SRC = join(import.meta.dirname, '..')

function sources(dir = SRC): string[] {
  return readdirSync(dir, { withFileTypes: true }).flatMap(entry => {
    const path = join(dir, entry.name)
    if (entry.isDirectory()) return sources(path)
    return /\.tsx?$/.test(entry.name) && !/\.test\.tsx?$/.test(entry.name) ? [relative(SRC, path)] : []
  })
}
const files = sources().map(path => ({ path, text: readFileSync(join(SRC, path), 'utf8') }))

function styleSheets(dir = SRC): string[] {
  return readdirSync(dir, { withFileTypes: true }).flatMap(entry => {
    const path = join(dir, entry.name)
    if (entry.isDirectory()) return styleSheets(path)
    return entry.name.endsWith('.css') ? [relative(SRC, path)] : []
  })
}
const sheets = styleSheets().map(path => ({
  path,
  text: readFileSync(join(SRC, path), 'utf8').replace(/\/\*[\s\S]*?\*\//g, ''),
}))
const code = (text: string) => text.replace(/\/\*[\s\S]*?\*\//g, '').replace(/(^|[^:'"`])\/\/.*$/gm, '$1')

/**
 * Where the `k-link` class may be written by hand: the component itself, and links that leave the app (they are not a
 * place in it, but look the same): the support address on the sign-in page, a provider's key page in Server admin.
 */
const OWN_LINK_LOOK = new Set(['ui/AppLink.tsx', 'Login.tsx', 'admin/ProviderDialog.tsx'])

test('the link look comes from AppLink or LinkButton, never a hand-made class', () => {
  const found = files
    .filter(f => !OWN_LINK_LOOK.has(f.path) && /className=["{][^"}]*\bk-link\b/.test(code(f.text)))
    .map(f => f.path)
  expect(found, 'Use AppLink (a place in the app) or LinkButton (an action) from ui/AppLink.tsx').toEqual([])
})

/**
 * Classes that may be underlined all the time, besides the link itself: HelpTip's "Learn more" (a page outside the
 * app, inside the tooltip's own look), and the help report's links (feedback/ReportDialog.tsx, being redesigned in its
 * own work; it moves to LinkButton there).
 */
const OWN_UNDERLINE = new Set(['k-link', 'k-help-more', 'fb-link'])

/** The classes of a selector's last part (the element it styles), without :not(…) and pseudo-classes. */
function styledClasses(selector: string): string[] {
  const last =
    selector
      .trim()
      .split(/\s*[\s>+~]\s*/)
      .at(-1) ?? ''
  return [...last.replace(/:not\([^)]*\)/g, '').matchAll(/\.([\w-]+)/g)].map(m => m[1]!)
}

test('no class draws the link look by hand: an underline that is always there is AppLink or LinkButton', () => {
  const found = sheets.flatMap(sheet =>
    [...sheet.text.matchAll(/([^{}]+)\{([^{}]*)\}/g)]
      .filter(([, , body]) => /text-decoration:\s*underline/.test(body!))
      .flatMap(([, selectors]) =>
        selectors!
          .split(',')
          .filter(selector => !/:(hover|focus|active)/.test(selector))
          .flatMap(styledClasses)
          .filter(name => !OWN_UNDERLINE.has(name))
          .map(name => `${sheet.path}: .${name}`),
      ),
  )
  expect(found, 'Use AppLink (a place in the app) or LinkButton (an action) from ui/AppLink.tsx').toEqual([])
})

test('a place named in a message is rendered by appLink or adminLink, not an <a> written in the tag', () => {
  const inAppTag = /chunks\s*=>\s*\(?\s*<a\b[^>]*href=(["'`{]+)#/
  const found = files.filter(f => inAppTag.test(code(f.text))).map(f => f.path)
  expect(found, "values={{ link: appLink('#…') }} (ui/AppLink.tsx)").toEqual([])
})