summaryrefslogtreecommitdiffhomepage
path: root/src/features/system-prompt/logic/view-model.test.ts
blob: 223327bfe5ca665ea284363e0fa9447584937799 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import type { SystemPromptVariable } from "@dispatch/transport-contract";
import { describe, expect, it } from "vitest";
import {
  buildIfNotTag,
  buildIfTag,
  buildTag,
  groupVariables,
  insertTag,
  isDynamicVariable,
} from "./view-model";

describe("system-prompt view-model", () => {
  describe("buildTag", () => {
    it("builds a variable placeholder", () => {
      expect(buildTag("system", "time")).toBe("[system:time]");
    });
  });

  describe("buildIfTag", () => {
    it("builds an opening conditional tag", () => {
      expect(buildIfTag("file", "AGENTS.md")).toBe("[if file:AGENTS.md]");
    });
  });

  describe("buildIfNotTag", () => {
    it("builds a negated opening conditional tag", () => {
      expect(buildIfNotTag("prompt", "cwd")).toBe("[if !prompt:cwd]");
    });
  });

  describe("insertTag", () => {
    it("inserts a tag at the cursor position", () => {
      expect(insertTag("Hello world", "[system:time]", 5, 5)).toEqual({
        template: "Hello[system:time] world",
        cursor: 18,
      });
    });

    it("replaces the selected text", () => {
      const tag = buildTag("file", "README.md");
      expect(insertTag("Hello world", tag, 6, 11)).toEqual({
        template: `Hello ${tag}`,
        cursor: 6 + tag.length,
      });
    });

    it("inserts at the end", () => {
      const tag = buildTag("git", "branch");
      expect(insertTag("", tag, 0, 0)).toEqual({
        template: tag,
        cursor: tag.length,
      });
    });
  });

  describe("groupVariables", () => {
    it("groups by type in first-appearing order", () => {
      const variables: SystemPromptVariable[] = [
        { type: "system", name: "time", description: "" },
        { type: "prompt", name: "cwd", description: "" },
        { type: "system", name: "date", description: "" },
        { type: "git", name: "branch", description: "" },
      ];
      const groups = groupVariables(variables);
      expect(groups.map((g) => g.type)).toEqual(["system", "prompt", "git"]);
      expect(groups[0]?.variables.map((v) => v.name)).toEqual(["time", "date"]);
      expect(groups[1]?.variables.map((v) => v.name)).toEqual(["cwd"]);
      expect(groups[2]?.variables.map((v) => v.name)).toEqual(["branch"]);
    });

    it("returns an empty array when no variables", () => {
      expect(groupVariables([])).toEqual([]);
    });
  });

  describe("isDynamicVariable", () => {
    it("returns true when dynamic is true", () => {
      expect(
        isDynamicVariable({ type: "file", name: "path", description: "", dynamic: true }),
      ).toBe(true);
    });

    it("returns false when dynamic is missing or false", () => {
      expect(isDynamicVariable({ type: "system", name: "time", description: "" })).toBe(false);
      expect(
        isDynamicVariable({ type: "system", name: "time", description: "", dynamic: false }),
      ).toBe(false);
    });
  });
});