Files
cc-switch/tests/components/sessionUtils.test.ts
T
ayxwi 6716a4c408 [codex] Fix VS Code session previews (#3593)
* Fix Codex VS Code session previews

* fix(codex): use last IDE request heading for session previews

A markdown heading inside the active selection / open file could precede the real injected request, so matching the first "## My request for Codex:" heading picked selection content instead of the user prompt. Scan for the last matching heading (the IDE injects the real request as the final section) on both the Rust title path and the frontend TOC preview path.

Add regression tests for the selection-heading case, and pin the known best-effort limitation when the request body itself repeats the heading.

---------

Co-authored-by: Jason <farion1231@gmail.com>
2026-06-07 19:23:24 +08:00

119 lines
3.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
extractCodexPromptPreview,
formatSessionMessagePreview,
shouldHideCodexMessageFromToc,
} from "@/components/sessions/utils";
describe("session utils", () => {
it("extracts Codex VS Code prompts after the request marker", () => {
const content = [
"# Context from my IDE setup:",
"",
"## Active file: src/main.ts",
"",
"## My request for Codex:",
"Fix the session title preview",
].join("\n");
expect(extractCodexPromptPreview(content)).toBe(
"Fix the session title preview",
);
});
it("extracts inline Codex VS Code prompts", () => {
const content = [
"# Context from my IDE setup:",
"",
"## My request for Codex: Fix the TOC preview",
].join("\n");
expect(extractCodexPromptPreview(content)).toBe("Fix the TOC preview");
});
it("ignores marker mentions before the Codex request heading", () => {
const content = [
"# Context from my IDE setup:",
"",
"## Active selection:",
"My request for Codex: not the prompt",
"",
"## My request for Codex:",
"Use the real request heading",
].join("\n");
expect(extractCodexPromptPreview(content)).toBe(
"Use the real request heading",
);
});
it("uses the last request heading when the selection contains one", () => {
const content = [
"# Context from my IDE setup:",
"",
"## Active selection: docs/codex-format.md:10-14",
"## My request for Codex:",
"selected document content, not the real request",
"",
"## My request for Codex:",
"the real injected request",
].join("\n");
expect(extractCodexPromptPreview(content)).toBe(
"the real injected request",
);
});
// Known limitation: the IDE marker is matched purely by text, so a
// "## My request for Codex:" line inside the real request body is treated as
// a new boundary and only the trailing part is kept. Pinning this documents
// the best-effort behavior; fully fixing it needs structured IDE section data
// that the Codex VS Code context does not provide.
it("keeps only the trailing part when the request body repeats the heading", () => {
const content = [
"# Context from my IDE setup:",
"",
"## Active file: foo.ts",
"",
"## My request for Codex:",
"Document the format, for example:",
"## My request for Codex:",
"and the rest follows.",
].join("\n");
expect(extractCodexPromptPreview(content)).toBe("and the rest follows.");
});
it("does not extract from ordinary messages that mention the marker", () => {
const content = "Please explain the phrase My request for Codex.";
expect(extractCodexPromptPreview(content)).toBe(content);
});
it("hides Codex context messages without user prompts from the TOC", () => {
expect(
shouldHideCodexMessageFromToc("# AGENTS.md instructions for F:/project"),
).toBe(true);
expect(
shouldHideCodexMessageFromToc(
"<environment_context>\n<cwd>F:/project</cwd>",
),
).toBe(true);
expect(shouldHideCodexMessageFromToc("# Context from my IDE setup:")).toBe(
true,
);
expect(
shouldHideCodexMessageFromToc(
"# Context from my IDE setup:\n\n## My request for Codex:\nFix it",
),
).toBe(false);
});
it("formats message previews with truncation", () => {
expect(formatSessionMessagePreview("short message")).toBe("short message");
expect(formatSessionMessagePreview("a".repeat(51))).toBe(
`${"a".repeat(50)}...`,
);
});
});