Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 202 additions & 0 deletions src/tests/utils/markdownEditorUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import {
parseLine,
detectPrefix,
allLinesHavePrefix,
createLineEditRange,
} from "../../utils/markdownEditorUtils";

describe("markdownEditorUtils", () => {
describe("parseLine", () => {
it("should return empty whitespace for line with no leading spaces", () => {
const result = parseLine("Hello World");
expect(result).toEqual({
leadingWhitespace: "",
content: "Hello World",
});
});

it("should extract leading spaces", () => {
const result = parseLine(" Hello");
expect(result).toEqual({
leadingWhitespace: " ",
content: "Hello",
});
});

it("should extract leading tabs", () => {
const result = parseLine("\t\tHello");
expect(result).toEqual({
leadingWhitespace: "\t\t",
content: "Hello",
});
});

it("should handle mixed whitespace", () => {
const result = parseLine(" \t Hello");
expect(result).toEqual({
leadingWhitespace: " \t ",
content: "Hello",
});
});

it("should handle empty line", () => {
const result = parseLine("");
expect(result).toEqual({
leadingWhitespace: "",
content: "",
});
});

it("should handle line with only whitespace", () => {
const result = parseLine(" ");
expect(result).toEqual({
leadingWhitespace: " ",
content: "",
});
});
});

describe("detectPrefix", () => {
it("should return undefined matched when no prefix found", () => {
const result = detectPrefix("Hello World", ["#", "##", "###"]);
expect(result).toEqual({
matched: undefined,
stripped: "Hello World",
});
});

it("should detect single hash heading prefix", () => {
const result = detectPrefix("# Hello", ["#", "##", "###"]);
expect(result).toEqual({
matched: "#",
stripped: "Hello",
});
});

it("should detect double hash heading prefix", () => {
const result = detectPrefix("## Hello", ["#", "##", "###"]);
expect(result).toEqual({
matched: "##",
stripped: "Hello",
});
});

it("should detect triple hash heading prefix", () => {
const result = detectPrefix("### Hello", ["#", "##", "###"]);
expect(result).toEqual({
matched: "###",
stripped: "Hello",
});
});

it("should detect unordered list prefix", () => {
const result = detectPrefix("- Item", ["-"]);
expect(result).toEqual({
matched: "-",
stripped: "Item",
});
});

it("should handle prefix with no content after it", () => {
const result = detectPrefix("#", ["#", "##"]);
expect(result).toEqual({
matched: "#",
stripped: "",
});
});

it("should not match prefix that is not in the list", () => {
const result = detectPrefix("* Item", ["-"]);
expect(result).toEqual({
matched: undefined,
stripped: "* Item",
});
});

it("should handle content with leading spaces before prefix", () => {
const result = detectPrefix(" # Hello", ["#", "##"]);
expect(result).toEqual({
matched: "#",
stripped: "Hello",
});
});

it("should not match when prefix is part of word", () => {
const result = detectPrefix("#hashtag", ["#"]);
expect(result).toEqual({
matched: undefined,
stripped: "#hashtag",
});
});
});

describe("allLinesHavePrefix", () => {
it("should return true when all lines have the same prefix", () => {
const lines = ["# Hello", "# World", "# Test"];
const result = allLinesHavePrefix(lines, "#", ["#", "##", "###"]);
expect(result).toBe(true);
});

it("should return false when lines have different prefixes", () => {
const lines = ["# Hello", "## World", "# Test"];
const result = allLinesHavePrefix(lines, "#", ["#", "##", "###"]);
expect(result).toBe(false);
});

it("should return false when some lines have no prefix", () => {
const lines = ["# Hello", "World", "# Test"];
const result = allLinesHavePrefix(lines, "#", ["#", "##", "###"]);
expect(result).toBe(false);
});

it("should handle single line", () => {
const lines = ["# Hello"];
const result = allLinesHavePrefix(lines, "#", ["#", "##", "###"]);
expect(result).toBe(true);
});

it("should handle lines with leading whitespace", () => {
const lines = [" # Hello", " # World"];
const result = allLinesHavePrefix(lines, "#", ["#", "##", "###"]);
expect(result).toBe(true);
});

it("should return true for empty array", () => {
const lines: string[] = [];
const result = allLinesHavePrefix(lines, "#", ["#", "##", "###"]);
expect(result).toBe(true);
});
});

describe("createLineEditRange", () => {
it("should create correct range for line 1", () => {
const result = createLineEditRange(1, 10);
expect(result).toEqual({
startLineNumber: 1,
startColumn: 1,
endLineNumber: 1,
endColumn: 11,
});
});

it("should create correct range for arbitrary line", () => {
const result = createLineEditRange(5, 20);
expect(result).toEqual({
startLineNumber: 5,
startColumn: 1,
endLineNumber: 5,
endColumn: 21,
});
});

it("should handle zero-length line", () => {
const result = createLineEditRange(3, 0);
expect(result).toEqual({
startLineNumber: 3,
startColumn: 1,
endLineNumber: 3,
endColumn: 1,
});
});
});
});
22 changes: 22 additions & 0 deletions src/utils/testing/__mocks__/monaco-editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Mock for monaco-editor module used in tests
* This allows testing utilities that import Monaco types without loading the full editor
*/

export class Selection {
constructor(
public startLineNumber: number,
public startColumn: number,
public endLineNumber: number,
public endColumn: number
) {}
}

export const editor = {};

Comment on lines +15 to +16
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mock currently exports only an empty editor object and a Selection class. Because the Vitest config aliases monaco-editor globally, any test that imports code using Monaco runtime APIs (e.g., monaco.languages.*, monaco.editor.*) will throw at runtime with this mock. Either expand the mock to include the minimal runtime shape used by the app (stub languages, editor, etc.), or avoid applying the alias globally and mock Monaco only in the tests that need it.

Suggested change
export const editor = {};
/**
* Minimal stub for the monaco.editor namespace used in tests.
* Functions are no-ops that return simple disposable-like objects
* to prevent runtime errors when test code calls Monaco APIs.
*/
export const editor = {
create: (_domElement?: unknown, _options?: unknown) => {
const model = {
getValue: (): string => '',
setValue: (_value: string): void => {
// no-op
},
dispose: (): void => {
// no-op
},
};
return {
onDidChangeModelContent: (
_listener: () => void
): { dispose: () => void } => ({
dispose: (): void => {
// no-op
},
}),
getValue: (): string => '',
setValue: (_value: string): void => {
// no-op
},
getModel: () => model,
updateOptions: (_options: unknown): void => {
// no-op
},
dispose: (): void => {
// no-op
},
};
},
createModel: (value: string, _language?: string) => ({
getValue: (): string => value,
setValue: (_newValue: string): void => {
// no-op
},
dispose: (): void => {
// no-op
},
}),
setModelLanguage: (_model: unknown, _languageId: string): void => {
// no-op
},
};
/**
* Minimal stub for the monaco.languages namespace used in tests.
*/
export const languages = {
register: (_language: unknown): void => {
// no-op
},
setMonarchTokensProvider: (_languageId: string, _tokensProvider: unknown): void => {
// no-op
},
registerCompletionItemProvider: (
_languageId: string,
_provider: unknown
): { dispose: () => void } => ({
dispose: (): void => {
// no-op
},
}),
};

Copilot uses AI. Check for mistakes.
export interface IRange {
startLineNumber: number;
startColumn: number;
endLineNumber: number;
endColumn: number;
}
7 changes: 7 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fileURLToPath } from "node:url";
import { defineConfig as defineViteConfig, mergeConfig } from "vite";
import { defineConfig as defineVitestConfig, configDefaults } from "vitest/config";
import react from "@vitejs/plugin-react";
Expand All @@ -23,6 +24,12 @@ const vitestConfig = defineVitestConfig({
environment: "jsdom",
setupFiles: "./src/utils/testing/setup.ts",
exclude: [...configDefaults.exclude, "**/e2e/**"],
deps: {
inline: ["monaco-editor"],
},
alias: {
"monaco-editor": fileURLToPath(new URL("./src/utils/testing/__mocks__/monaco-editor.ts", import.meta.url)),
},
Comment on lines +30 to +32
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Vitest config aliases the entire monaco-editor module to a local mock for all unit tests. This makes it hard to add future tests that rely on real Monaco behavior (or a richer mock) and can cause unrelated test failures when a component imports Monaco at runtime. Consider scoping the Monaco mocking to only the specific test(s) that need it (e.g., vi.mock('monaco-editor', ...) in the test file or in setup.ts with conditional logic), or alternatively remove the runtime Monaco import need by switching the util to import type (so no module-level Monaco load occurs during tests).

Suggested change
alias: {
"monaco-editor": fileURLToPath(new URL("./src/utils/testing/__mocks__/monaco-editor.ts", import.meta.url)),
},

Copilot uses AI. Check for mistakes.
},
});

Expand Down
Loading