test.build/core/converters/source.segment.converter.test.ts
test.build/core/converters/source.segment.converter.test.ts is a file in Codebase Testing. 61 lines of code and 0 definitions.
import { describe, expect, it } from "vitest";
import {
partPathOf,
partSlicesOf,
sourcePartsOf,
} from "@banes-lab/build-scripts/core/converters/source.segment.converter.ts";
import type { SourceRoute } from "@banes-lab/build-scripts/types/source.types.ts";
const ROUTE: SourceRoute = {
children: [],
parent: { label: "Site", path: "/anatomy/tree" },
subject: {
alternates: { json: "/json/source/tree/a.json", markdown: "/source/tree/a.json.md" },
description: "a.json is a file in Site.",
language: "json",
license: null,
name: "a.json",
path: "/anatomy/tree/file-a-json",
repository: null,
tabPath: "/anatomy/tree",
tabTitle: "Site",
title: "Site — a.json — Bane's Lab",
trail: [{ label: "Site", path: "/anatomy/tree" }],
},
text: "a.json.txt",
};
describe("partSlicesOf", () => {
it("cuts text at a line end within the limit, and cuts a line longer than the limit where it reaches it", () => {
expect(partSlicesOf("aaaa\nbbbb\ncc", 7)).toStrictEqual([
[0, 5],
[5, 12],
]);
expect(partSlicesOf("aaaa\nbbbb\ncc", 5)).toStrictEqual([
[0, 5],
[5, 10],
[10, 12],
]);
expect(partSlicesOf("abcdefghij", 4)).toStrictEqual([
[0, 4],
[4, 8],
[8, 10],
]);
expect(partSlicesOf("short", 10)).toStrictEqual([[0, 5]]);
});
});
describe("sourcePartsOf", () => {
it("keeps a file within the limit whole, and splits a longer one into linked numbered parts", () => {
expect(sourcePartsOf(ROUTE, "short", 10)).toStrictEqual([ROUTE]);
const parts = sourcePartsOf(ROUTE, "aaaa\nbbbb\ncc", 5);
expect(parts.map((part) => part.subject.path)).toStrictEqual([
ROUTE.subject.path,
partPathOf(ROUTE.subject.path, 1),
partPathOf(ROUTE.subject.path, 2),
]);
expect(partPathOf(ROUTE.subject.path, 1)).toBe("/anatomy/tree/file-a-json~2");
expect(parts[0]?.subject.title).toBe(ROUTE.subject.title);
expect(parts[1]?.subject.title).toBe("Part 2 of 3 — Site — a.json — Bane's Lab");
expect(parts[2]?.subject.description).toBe("a.json continued, part 3 of 3.");
expect(parts[1]?.slice).toStrictEqual([5, 10]);
expect(parts[0]?.children.map((link) => link.label)).toStrictEqual(["Part 1 of 3", "Part 2 of 3", "Part 3 of 3"]);
});
});