Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/keysmash/__tests__/text.test.ts

Raw
import { describe, expect, it } from "vitest";
import {
	indentTarget,
	insideFence,
	isEmptyPair,
	looksCompletable,
	outdentTarget,
	pairAction,
	parseItem,
} from "../src/text.ts";

describe("parseItem", () => {
	it("splits prefix, content, and the next prefix", () => {
		expect(parseItem("  1. hello")).toMatchObject({
			kind: "ordered",
			indent: 2,
			prefix: "  1. ",
			content: "hello",
			next: "  2. ",
			markerWidth: 3,
		});
		expect(parseItem("- [X]   done")).toMatchObject({
			kind: "task",
			prefix: "- [X]   ",
			content: "done",
			next: "- [ ]   ",
		});
		expect(parseItem("- [ ]")).toMatchObject({ kind: "task", content: "" });
		expect(parseItem("- [x]a")).toMatchObject({
			kind: "bullet",
			content: "[x]a",
		});
		expect(parseItem(">> deep")).toMatchObject({ kind: "quote", next: ">> " });
	});

	it("rejects ordinary text", () => {
		for (const line of ["", "text", "-x", "1.x", "*em*", ">x", "10 items"])
			expect(parseItem(line)).toBeUndefined();
	});
});

describe("insideFence", () => {
	it("tracks matching fences", () => {
		const lines = ["a", "~~~", "x", "```", "y", "~~~", "z"];
		expect(lines.map((_, i) => insideFence(lines, i))).toEqual([
			false,
			false,
			true,
			true,
			true,
			true,
			false,
		]);
	});

	it("ignores inline triple backticks", () => {
		expect(insideFence(["```code``` inline", "- a"], 1)).toBe(false);
	});
});

describe("indent targets", () => {
	it("stays bounded", () => {
		const deep = `${" ".repeat(31)}- a`;
		expect(indentTarget([deep], 0)).toBeUndefined();
		expect(outdentTarget(["- a"], 0)).toBeUndefined();
		expect(indentTarget(["> a"], 0)).toBeUndefined();
	});
});

describe("pairing rules", () => {
	it("recognizes paths and mentions as completion territory", () => {
		expect(looksCompletable("see @fi")).toBe(true);
		expect(looksCompletable("see src/a")).toBe(true);
		expect(looksCompletable("see words")).toBe(false);
	});

	it("decides on pair, overtype, and unpair", () => {
		expect(pairAction("(", "", "")).toBe("pair");
		expect(pairAction("(", "", "x")).toBeUndefined();
		expect(pairAction("(", "", ")")).toBe("pair");
		expect(pairAction(")", "(x", ")")).toBe("overtype");
		expect(pairAction(")", "x", "")).toBeUndefined();
		expect(isEmptyPair("a(", ")b")).toBe(true);
		expect(isEmptyPair("a(", "b")).toBe(false);
		expect(isEmptyPair("", "x")).toBe(false);
	});
});