activePromptInputToken.test.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createActivePromptInputTokenSnapshot,
  4. getActivePromptInputTokenReplacementRange,
  5. reconcileActivePromptInputTokenSnapshot,
  6. } from "../src/mentions/activePromptInputToken.js";
  7. function selection(text: string, cursorOffset: number, nodeKey = "node-1") {
  8. return {
  9. cursorOffset,
  10. nodeKey,
  11. text,
  12. textBeforeCursor: text.slice(0, cursorOffset),
  13. };
  14. }
  15. describe("active prompt input token", () => {
  16. it("左右移动仍在同一 token 内时保持原始 query 和完整范围", () => {
  17. const text = "@skill-creator";
  18. const snapshot = createActivePromptInputTokenSnapshot(selection(text, text.length));
  19. expect(snapshot).toMatchObject({
  20. nodeKey: "node-1",
  21. query: "skill-creator",
  22. tokenEnd: text.length,
  23. tokenStart: 0,
  24. tokenText: text,
  25. trigger: "@",
  26. });
  27. const movedLeft = reconcileActivePromptInputTokenSnapshot(snapshot, selection(text, 7), true);
  28. expect(movedLeft).toBe(snapshot);
  29. expect(
  30. reconcileActivePromptInputTokenSnapshot(movedLeft, selection(text, text.length), true),
  31. ).toBe(snapshot);
  32. expect(getActivePromptInputTokenReplacementRange(snapshot, selection(text, 7))).toEqual({
  33. end: text.length,
  34. start: 0,
  35. });
  36. });
  37. it("光标离开 token、节点改变或 token 文本改变时不复用快照", () => {
  38. const text = "前缀 @plugin-name 后文";
  39. const tokenEnd = text.indexOf(" 后文");
  40. const snapshot = createActivePromptInputTokenSnapshot(selection(text, tokenEnd));
  41. expect(snapshot).not.toBeNull();
  42. expect(
  43. reconcileActivePromptInputTokenSnapshot(snapshot, selection(text, text.indexOf("@")), true),
  44. ).toBeNull();
  45. expect(
  46. reconcileActivePromptInputTokenSnapshot(snapshot, selection(text, tokenEnd, "node-2"), true),
  47. ).toBeNull();
  48. const edited = text.replace("plugin-name", "plugin-next");
  49. expect(
  50. reconcileActivePromptInputTokenSnapshot(snapshot, selection(edited, tokenEnd), true),
  51. ).toMatchObject({ query: "plugin-next", tokenText: "@plugin-next" });
  52. });
  53. it("内容更新重新解析 query,裸触发符不会把后续正文纳入 token", () => {
  54. const original = createActivePromptInputTokenSnapshot(selection("@plugin", 7));
  55. expect(
  56. reconcileActivePromptInputTokenSnapshot(original, selection("@plug-in", 8), false),
  57. ).toMatchObject({ query: "plug-in", tokenText: "@plug-in" });
  58. const bareTrigger = createActivePromptInputTokenSnapshot(selection("@已有正文", 1));
  59. expect(bareTrigger).toMatchObject({
  60. query: "",
  61. tokenEnd: 1,
  62. tokenStart: 0,
  63. tokenText: "@",
  64. });
  65. expect(
  66. getActivePromptInputTokenReplacementRange(bareTrigger, selection("@已有正文", 1)),
  67. ).toEqual({ end: 1, start: 0 });
  68. });
  69. it("Slash token 位于既有 CJK 正文前时只替换用户实际输入范围", () => {
  70. const text = "/goal修复这个问题";
  71. const snapshot = createActivePromptInputTokenSnapshot(selection(text, "/goal".length));
  72. expect(snapshot).toMatchObject({
  73. query: "goal",
  74. tokenStart: 0,
  75. tokenEnd: "/goal".length,
  76. tokenText: "/goal",
  77. trigger: "/",
  78. });
  79. expect(
  80. getActivePromptInputTokenReplacementRange(
  81. snapshot,
  82. selection(text, "/goal".length - 1),
  83. ),
  84. ).toEqual({ start: 0, end: "/goal".length });
  85. });
  86. });