|
1 | | -import { EnvironmentId } from "@t3tools/contracts"; |
| 1 | +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
2 | 2 | import { searchableMessageSegments } from "@t3tools/shared/threadFindText"; |
3 | 3 | import { countThreadSearchOccurrences } from "@t3tools/shared/threadSearch"; |
| 4 | +import { AsyncResult } from "effect/reactivity"; |
4 | 5 | import { act, type ReactElement } from "react"; |
5 | 6 | import { createRoot, type Root } from "react-dom/client"; |
6 | 7 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
7 | 8 |
|
| 9 | +import { filesystemEnvironment } from "../state/filesystem"; |
| 10 | +import { useRightPanelStore } from "../rightPanelStore"; |
8 | 11 | import { getSyntaxHighlighterPromise } from "../lib/syntaxHighlighting"; |
9 | 12 | import { MarkdownFindContext } from "./chat/markdownFindContext"; |
10 | 13 | import { useThreadFindHighlights } from "./chat/threadFindHighlights"; |
11 | 14 | import { setMarkdownTaskChecked } from "./files/filePreviewMode"; |
12 | 15 |
|
| 16 | +const { workspaceSearch, filesystemMetadata } = vi.hoisted(() => ({ |
| 17 | + workspaceSearch: vi.fn(), |
| 18 | + filesystemMetadata: vi.fn(), |
| 19 | +})); |
| 20 | +vi.mock(import("../state/use-atom-query-runner"), async (original) => ({ |
| 21 | + ...(await original()), |
| 22 | + useAtomQueryRunner: ((family: unknown) => |
| 23 | + family === filesystemEnvironment.getMetadata ? filesystemMetadata : workspaceSearch) as never, |
| 24 | +})); |
| 25 | + |
13 | 26 | // Atoms derived from the server config run in the real registry over a missing config. |
14 | 27 | vi.mock(import("../state/server"), async (original) => { |
15 | 28 | const actual = await original(); |
@@ -47,6 +60,8 @@ import ChatMarkdown from "./ChatMarkdown"; |
47 | 60 | const roots: Root[] = []; |
48 | 61 |
|
49 | 62 | beforeEach(() => { |
| 63 | + workspaceSearch.mockReset().mockResolvedValue(AsyncResult.success({ entries: [] })); |
| 64 | + filesystemMetadata.mockReset().mockResolvedValue(AsyncResult.success({ entries: [null] })); |
50 | 65 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
51 | 66 | }); |
52 | 67 |
|
@@ -400,6 +415,165 @@ describe("ChatMarkdown skill chips", () => { |
400 | 415 | }); |
401 | 416 | }); |
402 | 417 |
|
| 418 | +describe("ChatMarkdown workspace file opens", () => { |
| 419 | + const threadRef = { |
| 420 | + environmentId: EnvironmentId.make("workspace"), |
| 421 | + threadId: ThreadId.make("thread"), |
| 422 | + }; |
| 423 | + |
| 424 | + it.each([ |
| 425 | + { |
| 426 | + cwd: "/school/courses/physics", |
| 427 | + path: "courses/physics/notes/a.md", |
| 428 | + indexedPath: "notes/a.md", |
| 429 | + }, |
| 430 | + { |
| 431 | + cwd: "C:/school/courses/Physics", |
| 432 | + path: "courses/physics/notes/a.md", |
| 433 | + indexedPath: "Notes/A.md", |
| 434 | + }, |
| 435 | + ])( |
| 436 | + "opens a repo-relative chip at its indexed path in $cwd", |
| 437 | + async ({ cwd, path, indexedPath }) => { |
| 438 | + workspaceSearch.mockResolvedValue( |
| 439 | + AsyncResult.success({ |
| 440 | + entries: [ |
| 441 | + { path: indexedPath, kind: "file" }, |
| 442 | + { path, kind: "file" }, |
| 443 | + ], |
| 444 | + truncated: false, |
| 445 | + }), |
| 446 | + ); |
| 447 | + const openFile = vi |
| 448 | + .spyOn(useRightPanelStore.getState(), "openFile") |
| 449 | + .mockImplementation(() => {}); |
| 450 | + const container = await renderText(`[Notes](${path}:7)`, { cwd, threadRef }); |
| 451 | + |
| 452 | + await act(async () => container.querySelector("a")!.click()); |
| 453 | + |
| 454 | + expect(openFile).toHaveBeenCalledExactlyOnceWith(threadRef, indexedPath, 7); |
| 455 | + }, |
| 456 | + ); |
| 457 | + |
| 458 | + it("prefers an existing literal nested file over the stripped Windows match", async () => { |
| 459 | + filesystemMetadata.mockResolvedValue( |
| 460 | + AsyncResult.success({ entries: [{ kind: "file", byteLength: 10 }] }), |
| 461 | + ); |
| 462 | + workspaceSearch.mockResolvedValue( |
| 463 | + AsyncResult.success({ |
| 464 | + entries: [ |
| 465 | + { path: "Notes/A.md", kind: "file" }, |
| 466 | + { path: "Physics/Notes/A.md", kind: "file" }, |
| 467 | + ], |
| 468 | + truncated: false, |
| 469 | + }), |
| 470 | + ); |
| 471 | + const openFile = vi |
| 472 | + .spyOn(useRightPanelStore.getState(), "openFile") |
| 473 | + .mockImplementation(() => {}); |
| 474 | + const container = await renderText("[Notes](physics/notes/a.md)", { |
| 475 | + cwd: "C:/school/Physics", |
| 476 | + threadRef, |
| 477 | + }); |
| 478 | + |
| 479 | + await act(async () => container.querySelector("a")!.click()); |
| 480 | + |
| 481 | + expect(openFile).toHaveBeenCalledExactlyOnceWith(threadRef, "physics/notes/a.md", undefined); |
| 482 | + }); |
| 483 | + |
| 484 | + it("keeps a literal file omitted by a truncated search", async () => { |
| 485 | + workspaceSearch.mockResolvedValue( |
| 486 | + AsyncResult.success({ |
| 487 | + entries: [{ path: "notes/a.md", kind: "file" }], |
| 488 | + truncated: true, |
| 489 | + }), |
| 490 | + ); |
| 491 | + filesystemMetadata.mockResolvedValue( |
| 492 | + AsyncResult.success({ entries: [{ kind: "file", byteLength: 10 }] }), |
| 493 | + ); |
| 494 | + const openFile = vi |
| 495 | + .spyOn(useRightPanelStore.getState(), "openFile") |
| 496 | + .mockImplementation(() => {}); |
| 497 | + const container = await renderText("[Notes](physics/notes/a.md:7)", { |
| 498 | + cwd: "/school/physics", |
| 499 | + threadRef, |
| 500 | + }); |
| 501 | + |
| 502 | + await act(async () => container.querySelector("a")!.click()); |
| 503 | + |
| 504 | + expect(openFile).toHaveBeenCalledExactlyOnceWith(threadRef, "physics/notes/a.md", 7); |
| 505 | + expect(filesystemMetadata).toHaveBeenCalledExactlyOnceWith({ |
| 506 | + environmentId: threadRef.environmentId, |
| 507 | + input: { paths: ["/school/physics/physics/notes/a.md"] }, |
| 508 | + }); |
| 509 | + }); |
| 510 | + |
| 511 | + it.each([ |
| 512 | + { |
| 513 | + kind: "absolute link", |
| 514 | + href: "/school/physics/physics/notes/a.md:7", |
| 515 | + imageBaseDir: undefined, |
| 516 | + }, |
| 517 | + { kind: "nested preview", href: "notes/a.md:7", imageBaseDir: "/school/physics/physics" }, |
| 518 | + ])( |
| 519 | + "keeps the literal target for a $kind when only its stripped twin exists", |
| 520 | + async ({ href, imageBaseDir }) => { |
| 521 | + workspaceSearch.mockResolvedValue( |
| 522 | + AsyncResult.success({ |
| 523 | + entries: [{ path: "notes/a.md", kind: "file" }], |
| 524 | + truncated: false, |
| 525 | + }), |
| 526 | + ); |
| 527 | + const openFile = vi |
| 528 | + .spyOn(useRightPanelStore.getState(), "openFile") |
| 529 | + .mockImplementation(() => {}); |
| 530 | + const container = await renderText(`[Notes](${href})`, { |
| 531 | + cwd: "/school/physics", |
| 532 | + imageBaseDir, |
| 533 | + threadRef, |
| 534 | + }); |
| 535 | + |
| 536 | + await act(async () => container.querySelector("a")!.click()); |
| 537 | + |
| 538 | + expect(openFile).toHaveBeenCalledExactlyOnceWith(threadRef, "physics/notes/a.md", 7); |
| 539 | + expect(workspaceSearch).not.toHaveBeenCalled(); |
| 540 | + expect(filesystemMetadata).not.toHaveBeenCalled(); |
| 541 | + }, |
| 542 | + ); |
| 543 | + |
| 544 | + it("keeps a newer absolute file open when an earlier repo-relative lookup finishes", async () => { |
| 545 | + const result = AsyncResult.success({ |
| 546 | + entries: [{ path: "notes/a.md", kind: "file" }], |
| 547 | + truncated: false, |
| 548 | + }); |
| 549 | + let resolvePending!: (value: typeof result) => void; |
| 550 | + const pending = new Promise<typeof result>((resolve) => { |
| 551 | + resolvePending = resolve; |
| 552 | + }); |
| 553 | + workspaceSearch.mockReturnValueOnce(pending); |
| 554 | + const openFile = vi |
| 555 | + .spyOn(useRightPanelStore.getState(), "openFile") |
| 556 | + .mockImplementation(() => {}); |
| 557 | + const container = await renderText( |
| 558 | + "[Notes](physics/notes/a.md) and [Other](/elsewhere/b.md:9)", |
| 559 | + { |
| 560 | + cwd: "/school/physics", |
| 561 | + threadRef, |
| 562 | + }, |
| 563 | + ); |
| 564 | + const [notes, other] = container.querySelectorAll("a"); |
| 565 | + await act(async () => notes!.click()); |
| 566 | + expect(workspaceSearch).toHaveBeenCalledOnce(); |
| 567 | + expect(openFile).not.toHaveBeenCalled(); |
| 568 | + |
| 569 | + await act(async () => other!.click()); |
| 570 | + await act(async () => resolvePending(result)); |
| 571 | + |
| 572 | + expect(openFile).toHaveBeenCalledExactlyOnceWith(threadRef, "/elsewhere/b.md", 9); |
| 573 | + expect(workspaceSearch).toHaveBeenCalledOnce(); |
| 574 | + }); |
| 575 | +}); |
| 576 | + |
403 | 577 | describe("ChatMarkdown file citations", () => { |
404 | 578 | it.each([true, false])( |
405 | 579 | "renders Codex file citations as file chips with parseRawHtml=%s", |
|
0 commit comments