File size: 2,118 Bytes
fd2c364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { beforeEach, describe, expect, it, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import {
  isLikelyDirectory,
  PathComponent,
} from "#/components/features/chat/path-component";

const openWorkspaceFile = vi.fn();

vi.mock("#/services/canvas-ui", () => ({
  openWorkspaceFile: (...args: unknown[]) => openWorkspaceFile(...args),
}));

vi.mock("#/hooks/use-conversation-id", () => ({
  useOptionalConversationId: () => ({ conversationId: "conv-1" }),
}));

describe("isLikelyDirectory", () => {
  it("should return false for empty path", () => {
    expect(isLikelyDirectory("")).toBe(false);
  });

  it("should return true for paths ending with forward slash", () => {
    expect(isLikelyDirectory("/path/to/dir/")).toBe(true);
    expect(isLikelyDirectory("dir/")).toBe(true);
  });

  it("should return true for paths ending with backslash", () => {
    expect(isLikelyDirectory("C:\\path\\to\\dir\\")).toBe(true);
    expect(isLikelyDirectory("dir\\")).toBe(true);
  });

  it("should return true for paths without extension", () => {
    expect(isLikelyDirectory("/path/to/dir")).toBe(true);
    expect(isLikelyDirectory("dir")).toBe(true);
  });

  it("should return false for paths ending with dot", () => {
    expect(isLikelyDirectory("/path/to/dir.")).toBe(false);
    expect(isLikelyDirectory("dir.")).toBe(false);
  });

  it("should return false for paths with file extensions", () => {
    expect(isLikelyDirectory("/path/to/file.txt")).toBe(false);
    expect(isLikelyDirectory("file.js")).toBe(false);
    expect(isLikelyDirectory("script.test.ts")).toBe(false);
  });
});

describe("PathComponent", () => {
  beforeEach(() => {
    openWorkspaceFile.mockClear();
  });

  it("opens the workspace file when the path is clicked", async () => {
    const user = userEvent.setup();
    const path = "docs/test.md";
    render(<PathComponent>{path}</PathComponent>);

    await user.click(screen.getByTestId("path-component-link"));

    expect(openWorkspaceFile).toHaveBeenCalledWith(path, "conv-1");
  });
});