| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- import { describe, expect, it } from "vitest";
- import {
- getGitPaneDiffPreviewPlan,
- shouldRenderPlainTextDiffPreview,
- } from "../src/GitPane/helpers.js";
- import { createPatchDiffOptions } from "../src/lib/patchDiffOptions.js";
- const SETTINGS = {
- lightTheme: "github-light",
- darkTheme: "github-dark",
- showLineNumbers: true,
- wrapLongLines: false,
- } as const;
- describe("createPatchDiffOptions", () => {
- it("keeps line-info separators by default", () => {
- const options = createPatchDiffOptions(SETTINGS, "light");
- expect(options.hunkSeparators).toBe("line-info");
- });
- it("supports simple separators for compact diff previews", () => {
- const options = createPatchDiffOptions(SETTINGS, "light", {
- separatorStyle: "simple",
- });
- expect(options.hunkSeparators).toBe("simple");
- });
- it("patch diff 与 worker 池使用同一个 WASM 高亮引擎", () => {
- const options = createPatchDiffOptions(SETTINGS, "dark");
- expect(options.preferredHighlighter).toBe("shiki-wasm");
- });
- });
- describe("shouldRenderPlainTextDiffPreview", () => {
- it("falls back for created plain text patches on review surfaces", () => {
- const patch = [
- "--- /dev/null",
- "+++ b/demo.txt",
- "@@ -0,0 +1,2 @@",
- "+hello",
- "+world",
- ].join("\n");
- expect(shouldRenderPlainTextDiffPreview(patch)).toEqual([
- "+hello",
- "+world",
- ]);
- });
- it("hides unified diff metadata and keeps no-newline markers", () => {
- const patch = [
- "--- /dev/null",
- "+++ b/demo.txt",
- "@@ -0,0 +1,1 @@",
- "+hello world",
- "\\ No newline at end of file",
- "",
- ].join("\n");
- expect(shouldRenderPlainTextDiffPreview(patch)).toEqual([
- "+hello world",
- "\\ No newline at end of file",
- ]);
- });
- it("keeps structured file types on PatchDiff path", () => {
- const patch = [
- "--- /dev/null",
- "+++ b/demo.json",
- "@@ -0,0 +1,1 @@",
- "+{}",
- ].join("\n");
- expect(shouldRenderPlainTextDiffPreview(patch)).toBeNull();
- });
- });
- describe("getGitPaneDiffPreviewPlan", () => {
- it("uses patch-only rendering when full before/after content is large", () => {
- const beforeContent = Array.from(
- { length: 1_300 },
- (_, index) => `line-${index + 1}`,
- ).join("\n");
- const afterContent = beforeContent.replace("line-10", "line-10 changed");
- expect(
- getGitPaneDiffPreviewPlan({
- path: "/workspace/demo.ts",
- availability: "patch",
- patch: [
- "--- a/demo.ts",
- "+++ b/demo.ts",
- "@@ -8,5 +8,5 @@",
- " line-8",
- " line-9",
- "-line-10",
- "+line-10 changed",
- " line-11",
- ].join("\n"),
- beforeContent,
- afterContent,
- summary: null,
- }),
- ).toEqual({ kind: "patch" });
- });
- it("uses plain text when a large created structured file would be expensive", () => {
- const afterLines = Array.from(
- { length: 1_300 },
- (_, index) => `+ "line-${index + 1}": true,`,
- );
- const plan = getGitPaneDiffPreviewPlan({
- path: "/workspace/demo.json",
- availability: "patch",
- patch: [
- "--- /dev/null",
- "+++ b/demo.json",
- "@@ -0,0 +1,1300 @@",
- ...afterLines,
- ].join("\n"),
- beforeContent: "",
- afterContent: afterLines.map((line) => line.slice(1)).join("\n"),
- summary: null,
- });
- expect(plan.kind).toBe("plain-text");
- expect(plan.kind === "plain-text" ? plan.lines.at(0) : null).toBe(
- '+ "line-1": true,',
- );
- });
- it("keeps unsafe patches on the lightweight fallback when full contents are unavailable", () => {
- const afterLines = Array.from(
- { length: 1_300 },
- (_, index) => `+ "line-${index + 1}": true,`,
- );
- const plan = getGitPaneDiffPreviewPlan({
- path: "/workspace/demo.json",
- availability: "patch",
- patch: [
- "--- /dev/null",
- "+++ b/demo.json",
- "@@ -0,0 +1,1300 @@",
- ...afterLines,
- ].join("\n"),
- beforeContent: null,
- afterContent: null,
- summary: null,
- });
- expect(plan.kind).toBe("plain-text");
- expect(plan.kind === "plain-text" ? plan.lines.at(0) : null).toBe(
- '+ "line-1": true,',
- );
- });
- });
|