-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.test.js
More file actions
150 lines (136 loc) · 4.26 KB
/
Copy pathextension.test.js
File metadata and controls
150 lines (136 loc) · 4.26 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
const assert = require("node:assert/strict");
const Module = require("node:module");
const path = require("node:path");
const test = require("node:test");
const extensionPath = path.resolve(__dirname, "../src/extension.js");
// Load extension.js with a fake `vscode`, a stubbed ./agentUsage, and a stubbed ./handoff. The
// returned `state` lets each test inspect registered commands, copied clipboard text, and
// notifications without touching the real VS Code API, git, or JSONL files.
function loadExtension({ usage }) {
delete require.cache[extensionPath];
const state = {
commands: {},
clipboardTexts: [],
informationMessages: [],
};
const fakeVscode = {
StatusBarAlignment: { Right: 1 },
ThemeColor: function ThemeColor(id) {
this.id = id;
},
env: {
clipboard: {
writeText(text) {
state.clipboardTexts.push(text);
return Promise.resolve();
},
},
},
commands: {
registerCommand(command, callback) {
state.commands[command] = callback;
return { dispose() {} };
},
},
window: {
createStatusBarItem() {
return { show() {}, hide() {} };
},
showInformationMessage(message) {
state.informationMessages.push(message);
},
},
workspace: {
getConfiguration() {
return {
get(_key, defaultValue) {
return defaultValue;
},
};
},
onDidChangeConfiguration() {
return { dispose() {} };
},
onDidChangeWorkspaceFolders() {
return { dispose() {} };
},
workspaceFolders: [],
},
};
const originalLoad = Module._load;
Module._load = function load(request, parent) {
if (request === "vscode") {
return fakeVscode;
}
if (parent && parent.filename === extensionPath) {
if (request === "./agentUsage") {
return {
formatAgentUsage() {
return { text: "Codex 3%", tooltip: "Codex: ctx", severity: "low" };
},
readLatestAgentUsage() {
return usage;
},
};
}
if (request === "./handoff") {
return {
buildHandoffPrompt: () => "HANDOFF_PROMPT",
collectGitInfo: () => ({ branch: "main", status: "", recentCommits: "abc x" }),
};
}
if (request === "./claudeStatuslineUsage") {
return {
readClaudeStatuslineUsage: () => null,
};
}
}
return originalLoad.call(this, request, parent);
};
const subscriptions = [];
const extension = require(extensionPath);
extension.activate({ subscriptions });
const restore = () => {
extension.deactivate();
subscriptions.forEach((subscription) => subscription.dispose && subscription.dispose());
Module._load = originalLoad;
delete require.cache[extensionPath];
};
return { state, extension, restore };
}
test("status bar click refreshes without showing a notification", () => {
const { state, restore } = loadExtension({ usage: { provider: "Codex" } });
try {
assert.equal(typeof state.commands["agentTokenStatus.refresh"], "function");
state.commands["agentTokenStatus.refresh"]();
assert.equal(state.informationMessages.length, 0);
} finally {
restore();
}
});
test("handoff command copies prompt to clipboard and notifies above threshold", async () => {
const { state, restore } = loadExtension({
usage: { provider: "Claude", contextPercent: 55 },
});
try {
assert.equal(typeof state.commands["agentTokenStatus.handoff"], "function");
await state.commands["agentTokenStatus.handoff"]();
assert.deepEqual(state.clipboardTexts, ["HANDOFF_PROMPT"]);
assert.equal(state.informationMessages.length, 1);
assert.match(state.informationMessages[0], /paste it into the session/i);
} finally {
restore();
}
});
test("handoff command does not trigger at the threshold, with a different message", async () => {
const { state, restore } = loadExtension({
usage: { provider: "Claude", contextPercent: 50 },
});
try {
await state.commands["agentTokenStatus.handoff"]();
assert.deepEqual(state.clipboardTexts, ["HANDOFF_PROMPT"]);
assert.match(state.informationMessages[0], /below the .* threshold/i);
} finally {
restore();
}
});