File size: 6,954 Bytes
76289e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/** Tests command-turn context normalization and source/kind conversions. */
import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import {
  createCommandTurnContext,
  isAuthorizedTextSlashCommandTurn,
  isExplicitCommandTurn,
  isNativeCommandTurn,
  resolveCommandTurnContext,
  resolveCommandTurnTargetSessionKey,
} from "./command-turn-context.js";
import { isExplicitCommandTurnContext } from "./command-turn-detection.js";

const emptyConfig = {} as const satisfies OpenClawConfig;

describe("resolveCommandTurnContext", () => {
  it("derives native command turns from legacy context fields", () => {
    expect(
      resolveCommandTurnContext({
        CommandSource: "native",
        CommandAuthorized: true,
        CommandBody: "/status now",
      }),
    ).toEqual({
      kind: "native",
      source: "native",
      authorized: true,
      commandName: "status",
      body: "/status now",
    });
  });

  it("derives text slash command turns from legacy context fields", () => {
    expect(
      resolveCommandTurnContext({
        CommandSource: "text",
        CommandAuthorized: true,
        CommandBody: "/model gpt-5.5",
      }),
    ).toMatchObject({
      kind: "text-slash",
      source: "text",
      authorized: true,
      commandName: "model",
    });
  });

  it("keeps normal message turns non-explicit even when command auth is true elsewhere", () => {
    const commandTurn = resolveCommandTurnContext({
      CommandAuthorized: true,
      CommandBody: "hello",
    });
    expect(commandTurn).toMatchObject({
      kind: "normal",
      source: "message",
      authorized: false,
    });
    expect(isExplicitCommandTurn(commandTurn)).toBe(false);
  });

  it("keeps an empty canonical command authoritative over stale aliases", () => {
    const input = {
      commandText: "",
      CommandBody: "/reset",
      CommandAuthorized: true,
    };

    expect(resolveCommandTurnContext(input)).toMatchObject({
      kind: "normal",
      body: "",
      commandName: undefined,
    });
    expect(isExplicitCommandTurnContext(input, emptyConfig)).toBe(false);
  });

  it("treats authorized control command bodies as explicit without legacy source tags", () => {
    expect(
      isExplicitCommandTurnContext(
        {
          CommandAuthorized: true,
          CommandBody: "/reset",
        },
        emptyConfig,
      ),
    ).toBe(true);
    expect(
      isExplicitCommandTurnContext(
        {
          CommandAuthorized: true,
          CommandBody: "hey can you /status please",
        },
        emptyConfig,
      ),
    ).toBe(false);
  });

  it("keeps structured normal command turns non-explicit", () => {
    expect(
      isExplicitCommandTurnContext(
        {
          CommandTurn: {
            kind: "normal",
            source: "message",
            authorized: false,
            body: "/think high through this",
          },
          CommandAuthorized: true,
          Body: "through this",
          RawBody: "through this",
          CommandBody: "/think high through this",
        },
        emptyConfig,
      ),
    ).toBe(false);
  });

  it("uses cleaned command bodies for command-shaped structured normal turns", () => {
    expect(
      isExplicitCommandTurnContext(
        {
          CommandTurn: {
            kind: "normal",
            source: "message",
            authorized: false,
            body: "/reset",
          },
          CommandAuthorized: true,
          Body: "/reset@openclaw",
          RawBody: "/reset@openclaw",
          CommandBody: "/reset",
        },
        emptyConfig,
      ),
    ).toBe(true);
  });

  it("normalizes bot-mentioned command bodies for structured normal turns", () => {
    expect(
      isExplicitCommandTurnContext(
        {
          CommandTurn: {
            kind: "normal",
            source: "message",
            authorized: false,
            body: "/reset@openclaw",
          },
          CommandAuthorized: true,
          Body: "/reset@openclaw",
          RawBody: "/reset@openclaw",
          CommandBody: "/reset@openclaw",
          BotUsername: "openclaw",
        },
        emptyConfig,
      ),
    ).toBe(true);
  });

  it("lets structured command turns override legacy command fields", () => {
    expect(
      resolveCommandTurnContext({
        CommandTurn: {
          kind: "text-slash",
          source: "text",
          authorized: false,
          commandName: "status",
          body: "/status",
        },
        CommandSource: "native",
        CommandAuthorized: true,
      }),
    ).toEqual({
      kind: "text-slash",
      source: "text",
      authorized: false,
      commandName: "status",
      body: "/status",
    });
  });

  it("rejects inconsistent structured command turn pairs", () => {
    expect(
      resolveCommandTurnContext({
        CommandTurn: {
          kind: "native",
          source: "message",
          authorized: true,
        },
        CommandSource: "text",
        CommandAuthorized: true,
        CommandBody: "/status",
      }),
    ).toMatchObject({
      kind: "text-slash",
      source: "text",
      authorized: true,
    });
  });

  it("exposes native/text helper predicates and target session resolution", () => {
    const nativeTurn = createCommandTurnContext("native", {
      authorized: true,
      body: "/stop",
    });
    const textTurn = createCommandTurnContext("text", {
      authorized: true,
      body: "/status",
    });

    expect(isNativeCommandTurn(nativeTurn)).toBe(true);
    expect(isAuthorizedTextSlashCommandTurn(textTurn)).toBe(true);
    expect(
      resolveCommandTurnTargetSessionKey({
        CommandTurn: nativeTurn,
        CommandTargetSessionKey: " target-session ",
      }),
    ).toBe("target-session");
    expect(
      resolveCommandTurnTargetSessionKey({
        CommandSource: "native",
        CommandAuthorized: true,
        CommandTargetSessionKey: " legacy-target ",
      }),
    ).toBe("legacy-target");
    expect(
      resolveCommandTurnTargetSessionKey({
        CommandTurn: createCommandTurnContext("text", {
          authorized: true,
          body: "/steer finish with a table",
          commandName: "steer",
        }),
        CommandTargetSessionKey: " active-direct-session ",
      }),
    ).toBe("active-direct-session");
    expect(
      resolveCommandTurnTargetSessionKey({
        CommandTurn: textTurn,
        CommandTargetSessionKey: "must-not-retarget-status",
      }),
    ).toBeUndefined();
    expect(
      resolveCommandTurnTargetSessionKey({
        CommandTurn: createCommandTurnContext("text", {
          authorized: false,
          body: "/steer denied",
          commandName: "steer",
        }),
        CommandTargetSessionKey: "must-not-retarget-unauthorized",
      }),
    ).toBeUndefined();
    expect(isExplicitCommandTurn(undefined)).toBe(false);
  });
});