-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearningComment.test.js
More file actions
179 lines (152 loc) ยท 5.26 KB
/
Copy pathlearningComment.test.js
File metadata and controls
179 lines (152 loc) ยท 5.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
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
import { describe, it, expect, beforeEach } from "bun:test";
import { upsertLearningStatusComment } from "../utils/learningComment.js";
const REPO_OWNER = "DaleStudy";
const REPO_NAME = "leetcode-study";
const PR_NUMBER = 42;
const APP_TOKEN = "fake-app-token";
const COMMENT_MARKER = "<!-- dalestudy-learning-status -->";
function ok(body) {
return Promise.resolve({
ok: true,
status: 200,
statusText: "OK",
json: () => Promise.resolve(body),
});
}
function parseBody(call) {
return JSON.parse(call.init.body).body;
}
describe("upsertLearningStatusComment โ usage history accumulation", () => {
let originalFetch;
beforeEach(() => {
originalFetch = globalThis.fetch;
});
it("posts a new comment with a single usage row when no prior comment exists", async () => {
const calls = [];
globalThis.fetch = (url, init = {}) => {
calls.push({ url: String(url), init });
if (init.method === "POST") return ok({});
return ok([]); // no existing comments
};
await upsertLearningStatusComment(
REPO_OWNER,
REPO_NAME,
PR_NUMBER,
`${COMMENT_MARKER}\n## body`,
APP_TOKEN,
{ prompt_tokens: 100, completion_tokens: 50 }
);
const post = calls.find((c) => c.init.method === "POST");
expect(post).toBeDefined();
const body = parseBody(post);
expect(body).toContain("๐ข API ์ฌ์ฉ๋ (gpt-4.1-nano)");
expect(body).toContain("| 1 | 100 | 50 | 150 |");
// single-row history => no totals row
expect(body).not.toContain("**ํฉ๊ณ**");
// hidden marker stores an array
expect(body).toMatch(/<!-- usage-data: \[\{"prompt":100,"completion":50\}\] -->/);
globalThis.fetch = originalFetch;
});
it("accumulates usage rows across PR updates when prior usage marker is present", async () => {
const previousBody = [
COMMENT_MARKER,
"## existing body",
"",
`<!-- usage-data: [{"prompt":100,"completion":50},{"prompt":200,"completion":80}] -->`,
].join("\n");
const calls = [];
globalThis.fetch = (url, init = {}) => {
const u = String(url);
calls.push({ url: u, init });
if (u.includes(`/issues/${PR_NUMBER}/comments`) && (!init.method || init.method === "GET")) {
return ok([
{
id: 999,
user: { type: "Bot" },
body: previousBody,
},
]);
}
if (init.method === "PATCH") return ok({});
return ok([]);
};
await upsertLearningStatusComment(
REPO_OWNER,
REPO_NAME,
PR_NUMBER,
`${COMMENT_MARKER}\n## new body`,
APP_TOKEN,
{ prompt_tokens: 300, completion_tokens: 120 }
);
const patch = calls.find((c) => c.init.method === "PATCH");
expect(patch).toBeDefined();
expect(patch.url).toContain("/issues/comments/999");
const body = parseBody(patch);
// all three calls present, in order
expect(body).toContain("| 1 | 100 | 50 | 150 |");
expect(body).toContain("| 2 | 200 | 80 | 280 |");
expect(body).toContain("| 3 | 300 | 120 | 420 |");
// totals row appears once history.length > 1
expect(body).toContain("| **ํฉ๊ณ** | **600** | **250** | **850** |");
// marker is rewritten with the full array
expect(body).toMatch(
/<!-- usage-data: \[\{"prompt":100,"completion":50\},\{"prompt":200,"completion":80\},\{"prompt":300,"completion":120\}\] -->/
);
globalThis.fetch = originalFetch;
});
it("falls back to a single-row history when the prior marker is malformed", async () => {
// legacy / corrupt marker (object instead of array) โ should not crash, just reset
const previousBody = [
COMMENT_MARKER,
"## existing body",
"",
`<!-- usage-data: {"prompt":100,"completion":50} -->`,
].join("\n");
const calls = [];
globalThis.fetch = (url, init = {}) => {
const u = String(url);
calls.push({ url: u, init });
if (u.includes(`/issues/${PR_NUMBER}/comments`) && (!init.method || init.method === "GET")) {
return ok([
{ id: 1, user: { type: "Bot" }, body: previousBody },
]);
}
if (init.method === "PATCH") return ok({});
return ok([]);
};
await upsertLearningStatusComment(
REPO_OWNER,
REPO_NAME,
PR_NUMBER,
`${COMMENT_MARKER}\n## new body`,
APP_TOKEN,
{ prompt_tokens: 999, completion_tokens: 111 }
);
const patch = calls.find((c) => c.init.method === "PATCH");
const body = parseBody(patch);
expect(body).toContain("| 1 | 999 | 111 | 1,110 |");
expect(body).not.toContain("**ํฉ๊ณ**");
globalThis.fetch = originalFetch;
});
it("omits the usage section entirely when no usage is provided", async () => {
const calls = [];
globalThis.fetch = (url, init = {}) => {
calls.push({ url: String(url), init });
if (init.method === "POST") return ok({});
return ok([]);
};
await upsertLearningStatusComment(
REPO_OWNER,
REPO_NAME,
PR_NUMBER,
`${COMMENT_MARKER}\n## body`,
APP_TOKEN
// no usage
);
const post = calls.find((c) => c.init.method === "POST");
const body = parseBody(post);
expect(body).not.toContain("๐ข API ์ฌ์ฉ๋");
expect(body).not.toContain("usage-data:");
globalThis.fetch = originalFetch;
});
});