import { describe, expect, test } from "bun:test"; import { highlightLine, type Token } from "./snippet-highlight"; const kinds = (tokens: Token[]) => tokens.filter((token) => token.text.trim()).map((token) => [token.kind, token.text]); describe("highlightLine bash", () => { test("marks commands, flags, strings and operators", () => { expect(kinds(highlightLine(`git clone https://x.git gitfor && cd gitfor`, "bash"))).toEqual([ ["keyword", "git"], ["plain", " clone https://x.git gitfor "], ["sign", "&&"], ["keyword", "cd"], ["plain", " gitfor"], ]); expect(kinds(highlightLine(`echo "A=b" > .dev.vars`, "bash"))).toEqual([ ["keyword", "echo"], ["string", '"A=b"'], ["sign", ">"], ["plain", " .dev.vars"], ]); expect(kinds(highlightLine("gfd runner register --url https://x", "bash"))).toEqual([ ["keyword", "gfd"], ["plain", " runner register "], ["property", "--url"], ["plain", " https://x"], ]); }); test("comments take the whole line", () => { expect(kinds(highlightLine("# On any machine", "bash"))).toEqual([["comment", "# On any machine"]]); }); test("round-trips the source text", () => { const line = ` bunx wrangler secret put SECRETS_KEY # note`; expect( highlightLine(line, "bash") .map((token) => token.text) .join(""), ).toBe(line); }); }); describe("highlightLine yaml", () => { test("keys, list items, expressions and scalars", () => { expect(kinds(highlightLine(" runs-on: [self-hosted, linux]", "yaml"))).toEqual([ ["property", "runs-on"], ["sign", ":"], ["sign", "["], ["plain", "self-hosted"], ["sign", ","], ["plain", " linux"], ["sign", "]"], ]); expect(kinds(highlightLine(" - run: ./deploy.sh", "yaml"))).toEqual([ ["sign", "- "], ["property", "run"], ["sign", ":"], ["plain", " ./deploy.sh"], ]); expect(kinds(highlightLine(" TOKEN: ${{ secrets.DEPLOY_TOKEN }}", "yaml"))).toEqual([ ["property", "TOKEN"], ["sign", ":"], ["entity", "${{ secrets.DEPLOY_TOKEN }}"], ]); expect(kinds(highlightLine(" node: [22, 24]", "yaml"))).toContainEqual(["keyword", "22"]); }); test("round-trips the source text", () => { const line = " if: github.ref == 'refs/heads/main'"; expect( highlightLine(line, "yaml") .map((token) => token.text) .join(""), ).toBe(line); }); });