forked from zhzy0077/opencode-copilot-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot.test.mjs
More file actions
175 lines (152 loc) · 5.5 KB
/
copilot.test.mjs
File metadata and controls
175 lines (152 loc) · 5.5 KB
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
import { describe, expect, test } from "bun:test";
import { __testing } from "./index.mjs";
const { isSynthetic, detectAgent, detectVision, getConversationMetadata } = __testing;
function getInitiator(body) {
const messages = body?.messages || body?.input || [];
return detectAgent(messages) ? "agent" : "user";
}
describe("plugin.copilot", () => {
describe("isSynthetic", () => {
test("detects tool attachment pattern", () => {
expect(isSynthetic("Tool read_file returned an attachment:")).toBe(true);
expect(isSynthetic("Tool bash returned an attachment:")).toBe(true);
});
test("detects compaction pattern", () => {
expect(isSynthetic("What did we do so far?")).toBe(true);
expect(isSynthetic("What did we do so far? ")).toBe(true);
});
test("detects subtask pattern", () => {
expect(isSynthetic("The following tool was executed by the user")).toBe(true);
});
test("detects generic tool result patterns", () => {
expect(isSynthetic("Tool result: command finished")).toBe(true);
expect(isSynthetic("Tool output: hello")).toBe(true);
});
test("ignores normal user messages", () => {
expect(isSynthetic("Hello, can you help me?")).toBe(false);
expect(isSynthetic("Read the file README.md")).toBe(false);
expect(isSynthetic("What did we do yesterday?")).toBe(false);
});
test("handles empty and invalid input", () => {
expect(isSynthetic("")).toBe(false);
expect(isSynthetic(null)).toBe(false);
expect(isSynthetic(undefined)).toBe(false);
});
});
describe("detectAgent", () => {
test("first user message returns user", () => {
const body = { messages: [{ role: "user", content: "Hello" }] };
expect(getInitiator(body)).toBe("user");
});
test("empty messages returns user", () => {
expect(getInitiator({ messages: [] })).toBe("user");
expect(getInitiator({})).toBe("user");
expect(getInitiator(null)).toBe("user");
});
test("assistant message returns agent", () => {
const body = {
messages: [
{ role: "user", content: "Hello" },
{ role: "assistant", content: "Hi there" },
],
};
expect(getInitiator(body)).toBe("agent");
});
test("tool message returns agent", () => {
const body = {
messages: [
{ role: "user", content: "Run test" },
{ role: "tool", content: "Test passed" },
],
};
expect(getInitiator(body)).toBe("agent");
});
test("multiple user messages without assistant returns user", () => {
const body = {
messages: [
{ role: "user", content: "First" },
{ role: "user", content: "Second" },
],
};
expect(getInitiator(body)).toBe("user");
});
test("synthetic tool attachment returns agent", () => {
const body = {
messages: [{ role: "user", content: "Tool read_file returned an attachment:" }],
};
expect(getInitiator(body)).toBe("agent");
});
test("synthetic compaction returns agent", () => {
const body = {
messages: [{ role: "user", content: "What did we do so far? " }],
};
expect(getInitiator(body)).toBe("agent");
});
test("synthetic with array content returns agent", () => {
const body = {
messages: [
{
role: "user",
content: [
{ type: "text", text: "Tool bash returned an attachment:" },
{ type: "file", url: "file://out.txt" },
],
},
],
};
expect(getInitiator(body)).toBe("agent");
});
test("responses API agent types return agent", () => {
expect(getInitiator({ input: [{ role: "user", content: "Hello" }] })).toBe("user");
expect(getInitiator({ input: [{ type: "function_call", content: [] }] })).toBe("agent");
});
});
describe("detectVision", () => {
test("detects completions API image content anywhere in messages", () => {
expect(
detectVision([
{ role: "user", content: "hello" },
{ role: "user", content: [{ type: "image_url", image_url: { url: "x" } }] },
]),
).toBe(true);
});
test("detects responses API image content anywhere in input", () => {
expect(
detectVision([
{ role: "user", content: "hello" },
{ role: "user", content: [{ type: "input_image", image_url: "x" }] },
]),
).toBe(true);
});
});
describe("regression", () => {
test("synthetic user message after conversation does not charge premium", () => {
const body = {
messages: [
{ role: "user", content: "Read file.txt" },
{ role: "assistant", content: "Reading..." },
{ role: "user", content: "Tool read_file returned an attachment:" },
],
};
expect(getInitiator(body)).toBe("agent");
});
test("conversation metadata uses unified detection for messages", () => {
expect(
getConversationMetadata({
body: JSON.stringify({
messages: [{ role: "user", content: "What did we do so far?" }],
}),
}),
).toEqual({ isVision: false, isAgent: true });
});
test("conversation metadata uses unified detection for input", () => {
expect(
getConversationMetadata({
body: {
input: [{ role: "user", content: [{ type: "input_image", image_url: "x" }] }],
},
}),
).toEqual({ isVision: true, isAgent: false });
});
});
});