Skip to content

Add tests for codeblock-utils (7 pure functions, no test file) #883

Description

@TortoiseWolfe

The task

src/utils/codeblock-utils.ts has seven exported functions and no test file. Add one.

Why this is a good first issue

Every function in it is pure: same input, same output, no DOM, no network, no Supabase. You can
run it in isolation and reason about it entirely from the file. Verified — the module contains
zero references to document, window, fetch, localStorage or a database client.

The functions

function signature
generateBlockId (content: string) => string
countLines (text: string) => number
extractLanguage (className: string) => string | null
formatLineNumber (num: number, totalLines: number) => string
stripLineNumbers (content: string) => string
getLanguageDisplayName (language: string) => string
detectLanguage (content: string) => string

Where the test goes

src/utils/codeblock-utils.test.ts, beside the source. Look at any existing *.test.ts for the
house style — Vitest, describe / it / expect.

Acceptance criteria

  • A test file exists at src/utils/codeblock-utils.test.ts
  • Every one of the seven exports has at least one case
  • Each function has a case for its awkward input as well as its happy path — an empty
    string, a className with no language in it, formatLineNumber(9, 100) where padding
    matters, content that has no line numbers to strip
  • docker compose exec scripthammer pnpm test --run src/utils/codeblock-utils.test.ts passes

The one thing that matters here

Before you open the PR, break the function on purpose and check your test goes red. Change a
+ 1 to a - 1, return an empty string, whatever. Then undo it.

A test that passes but would also pass against broken code is worse than no test, because it
tells the next person the function is covered. This repo has a whole catalogue of those (#396),
and a gate that could not fail is the single most common defect we've found in it. Say in your
PR which mutation you tried — that's the most valuable line in the description.

Getting set up

Docker-first, always — never npm install or pnpm install on your host machine:

docker compose up -d
docker compose exec scripthammer pnpm test --run src/utils/codeblock-utils.test.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions