Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

2.8 KB
Raw Blame
import { describe, expect, it } from "vitest";
import {
	formatLoomCsvCell,
	isLoomCsvResultsFile,
	LOOM_CSV_RESULTS_HEADERS,
	parseConciergeLoomCsv,
	parseLoomCsvRecords,
	readLoomCsvCell,
} from "@/lib/loom-csv";

describe("concierge Loom CSV", () => {
	it("keeps quoted workspace names and source row numbers", () => {
		const rows = parseConciergeLoomCsv(
			"\uFEFFuser_email,space_name,loom_video_url\r\n" +
				"[email protected]," +
				JSON.stringify("Sales, Europe") +
				",https://www.loom.com/share/0123456789abcdef\r\n",
		);
		expect(rows).toEqual([
			{
				rowNumber: 2,
				loomUrl: "https://www.loom.com/share/0123456789abcdef",
				userEmail: "[email protected]",
				spaceName: "Sales, Europe",
			},
		]);
	});

	it("rejects a file without the mapping columns", () => {
		expect(() => parseConciergeLoomCsv("url,email\nlink,[email protected]")).toThrow(
			"loom_video_url and user_email",
		);
	});
});

describe("Loom CSV results cells", () => {
	it("keeps plain values unchanged", () => {
		expect(formatLoomCsvCell("[email protected]")).toBe("[email protected]");
		expect(
			formatLoomCsvCell("https://www.loom.com/share/0123456789abcdef"),
		).toBe("https://www.loom.com/share/0123456789abcdef");
	});

	it("quotes delimiters and doubles embedded quotes", () => {
		expect(formatLoomCsvCell("Sales, Europe")).toBe('"Sales, Europe"');
		expect(formatLoomCsvCell('The "A" team')).toBe('"The ""A"" team"');
	});

	it.each([
		["=HYPERLINK(1)", "'=HYPERLINK(1)"],
		["+1", "'+1"],
		["-1", "'-1"],
		["@SUM(A1)", "'@SUM(A1)"],
		["\tTab", "'\tTab"],
	])("neutralizes the formula prefix in %j", (value, expected) => {
		expect(formatLoomCsvCell(value)).toBe(expected);
	});

	it("neutralizes formulas that also need quoting", () => {
		expect(formatLoomCsvCell("=1,2")).toBe(`"'=1,2"`);
		expect(formatLoomCsvCell("\r=1")).toBe(`"'\r=1"`);
	});
});

describe("Loom CSV results round trip", () => {
	it("restores every value when a results CSV is imported again", () => {
		const values = [
			"@Sales",
			"[email protected]",
			"-Ops, EU",
			"=1",
			"'@Sales",
			"''=1",
			"'Sales",
			"O'Brien",
			"Sales",
		];
		const [cells] = parseLoomCsvRecords(
			`${values.map(formatLoomCsvCell).join(",")}\n`,
		);
		expect(cells?.map(readLoomCsvCell)).toEqual(values);
	});

	it("leaves apostrophes alone when no formula character follows", () => {
		expect(readLoomCsvCell("'Sales")).toBe("'Sales");
		expect(readLoomCsvCell("O'Brien")).toBe("O'Brien");
	});

	it("recognizes the results CSV layout regardless of column order or case", () => {
		expect(isLoomCsvResultsFile([...LOOM_CSV_RESULTS_HEADERS])).toBe(true);
		expect(
			isLoomCsvResultsFile([
				"Status",
				"Error",
				"Row",
				"User_Email",
				"Space_Name",
				"Loom_Video_URL",
			]),
		).toBe(true);
		expect(
			isLoomCsvResultsFile(["loom_video_url", "user_email", "space_name"]),
		).toBe(false);
	});
});