77 lines · 2.5 KB
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);
  });