CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | /**
* AI Standup tests.
*
* Pure-helper coverage runs unconditionally. The DB-backed branch
* (notification insertion + same-day dedupe) is gated on DATABASE_URL
* matching the project convention — see api-tokens.test.ts and friends.
*/
import { describe, it, expect } from "bun:test";
import {
__test,
classifyMaterial,
deliverStandup,
generateStandup,
hasStandupForToday,
utcDayKey,
} from "../lib/ai-standup";
const HAS_DB = Boolean(process.env.DATABASE_URL);
// ---------------------------------------------------------------------------
// Pure helpers — no DB, no AI client.
// ---------------------------------------------------------------------------
describe("ai-standup — utcDayKey", () => {
it("returns YYYY-MM-DD for a Date", () => {
expect(utcDayKey(new Date("2026-05-25T08:00:00.000Z"))).toBe(
"2026-05-25"
);
});
it("buckets two timestamps on the same UTC day to the same key", () => {
const a = new Date("2026-05-25T01:00:00.000Z");
const b = new Date("2026-05-25T23:59:59.000Z");
expect(utcDayKey(a)).toBe(utcDayKey(b));
});
});
describe("ai-standup — classifyMaterial", () => {
const now = new Date("2026-05-25T09:00:00.000Z");
const fourDaysAgo = new Date(now.getTime() - 4 * 24 * 60 * 60 * 1000);
const oneHourAgo = new Date(now.getTime() - 60 * 60 * 1000);
it("buckets merged PRs as shipped", () => {
const out = classifyMaterial({
now,
deploys: [],
issues: [],
prs: [
{
id: "p1",
number: 12,
title: "Add /metrics",
state: "merged",
isAiBuilt: false,
mergedAt: oneHourAgo,
updatedAt: oneHourAgo,
createdAt: oneHourAgo,
repo: "demo/repo",
},
],
});
expect(out.shipped.length).toBe(1);
expect(out.shipped[0]).toContain("Merged");
expect(out.inFlight.length).toBe(0);
expect(out.atRisk.length).toBe(0);
});
it("flags open PRs older than 3 days as at-risk", () => {
const out = classifyMaterial({
now,
deploys: [],
issues: [],
prs: [
{
id: "p1",
number: 7,
title: "WIP refactor",
state: "open",
isAiBuilt: false,
mergedAt: null,
updatedAt: fourDaysAgo,
createdAt: fourDaysAgo,
repo: "demo/repo",
},
],
});
expect(out.atRisk.length).toBe(1);
expect(out.atRisk[0]).toContain("Stale");
expect(out.inFlight.length).toBe(0);
});
it("surfaces ai:build PRs in the aiHighlights bucket", () => {
const out = classifyMaterial({
now,
deploys: [],
issues: [],
prs: [
{
id: "p1",
number: 1,
title: "ai:build add tests for /standups",
state: "merged",
isAiBuilt: true,
mergedAt: oneHourAgo,
updatedAt: oneHourAgo,
createdAt: oneHourAgo,
repo: "demo/repo",
},
],
});
expect(out.aiHighlights.length).toBe(1);
});
it("counts failed deploys as at-risk and succeeded as shipped", () => {
const out = classifyMaterial({
now,
issues: [],
prs: [],
deploys: [
{
runId: "r1",
sha: "deadbeefdeadbeef",
status: "succeeded",
startedAt: oneHourAgo,
finishedAt: oneHourAgo,
},
{
runId: "r2",
sha: "abcdefabcdefabcd",
status: "failed",
startedAt: oneHourAgo,
finishedAt: oneHourAgo,
},
],
});
expect(out.shipped.some((s) => s.includes("succeeded"))).toBe(true);
expect(out.atRisk.some((s) => s.includes("failed"))).toBe(true);
});
});
describe("ai-standup — renderFallbackSummary", () => {
it("renders all three sections when material is empty", () => {
const out = __test.renderFallbackSummary("daily", {
shipped: [],
inFlight: [],
atRisk: [],
aiHighlights: [],
});
expect(out).toContain("Daily standup");
expect(out).toContain("Shipped");
expect(out).toContain("In flight");
expect(out).toContain("At risk");
});
it("includes the AI section only when highlights exist", () => {
const empty = __test.renderFallbackSummary("daily", {
shipped: ["one"],
inFlight: [],
atRisk: [],
aiHighlights: [],
});
expect(empty).not.toContain("AI-driven changes");
const filled = __test.renderFallbackSummary("daily", {
shipped: [],
inFlight: [],
atRisk: [],
aiHighlights: ["AI-authored PR #1"],
});
expect(filled).toContain("AI-driven changes");
});
});
describe("ai-standup — buildPrompt", () => {
it("includes scope-specific wording", () => {
const daily = __test.buildPrompt("daily", {
shipped: [],
inFlight: [],
atRisk: [],
aiHighlights: [],
});
expect(daily).toContain("last 24 hours");
const weekly = __test.buildPrompt("weekly", {
shipped: [],
inFlight: [],
atRisk: [],
aiHighlights: [],
});
expect(weekly).toContain("last 7 days");
});
});
describe("ai-standup — deliverStandup (canned generator, DI'd)", () => {
it("dedupes when alreadyDelivered returns true", async () => {
let generated = 0;
const res = await deliverStandup({
userId: "u-1",
scope: "daily",
alreadyDelivered: async () => true,
generate: async () => {
generated += 1;
return {
summary: "should not be called",
shippedItems: [],
blockedItems: [],
atRiskItems: [],
windowStart: new Date(),
windowEnd: new Date(),
aiAvailable: false,
};
},
});
expect(generated).toBe(0);
expect(res.ok).toBe(false);
expect(res.reason).toContain("already");
expect(res.notified).toBe(false);
});
});
// ---------------------------------------------------------------------------
// DB-backed: insert a standup, then confirm the notification + dedupe.
// ---------------------------------------------------------------------------
describe("ai-standup — DB-backed delivery", () => {
it.skipIf(!HAS_DB)(
"creates a notification + dedupes on the same UTC day",
async () => {
const { db } = await import("../db");
const { users, notifications } = await import("../db/schema");
const { aiStandups, userStandupPrefs } = await import(
"../db/schema-standup"
);
const { eq } = await import("drizzle-orm");
const uname = "stand-" + Math.random().toString(36).slice(2, 10);
const [user] = await db
.insert(users)
.values({
username: uname,
email: `${uname}@example.com`,
passwordHash: "x",
})
.returning();
try {
const cannedGen = async () => ({
summary: "## 🚀 Shipped\n- demo\n\n## 🚧 In flight\n- nothing\n\n## ⚠️ At risk\n- nothing",
shippedItems: ["demo PR"],
blockedItems: [],
atRiskItems: [],
windowStart: new Date(Date.now() - 24 * 3600 * 1000),
windowEnd: new Date(),
aiAvailable: true,
});
// First call should insert + notify.
const first = await deliverStandup({
userId: user.id,
scope: "daily",
generate: cannedGen,
});
expect(first.ok).toBe(true);
expect(first.notified).toBe(true);
expect(first.standupId).toBeTruthy();
// The notification row should exist with the standup body and a
// /standups URL.
const notifs = await db
.select()
.from(notifications)
.where(eq(notifications.userId, user.id));
expect(notifs.length).toBeGreaterThanOrEqual(1);
const standupNotif = notifs.find((n) =>
(n.url || "").startsWith("/standups")
);
expect(standupNotif).toBeTruthy();
expect(standupNotif?.body || "").toContain("Shipped");
// Second call on the same UTC day should be deduped.
const second = await deliverStandup({
userId: user.id,
scope: "daily",
generate: cannedGen,
});
expect(second.ok).toBe(false);
expect(second.reason).toContain("already");
// hasStandupForToday should also report true.
const dupe = await hasStandupForToday(user.id, "daily", new Date());
expect(dupe).toBe(true);
} finally {
// Clean up rows we created — best-effort.
try {
await db
.delete(aiStandups)
.where(eq(aiStandups.userId, user.id));
} catch {
/* ignore */
}
try {
await db
.delete(userStandupPrefs)
.where(eq(userStandupPrefs.userId, user.id));
} catch {
/* ignore */
}
try {
await db
.delete(notifications)
.where(eq(notifications.userId, user.id));
} catch {
/* ignore */
}
try {
await db.delete(users).where(eq(users.id, user.id));
} catch {
/* ignore */
}
}
}
);
it.skipIf(!HAS_DB)(
"generateStandup returns a fallback body when AI key is absent",
async () => {
// Save and clear the key for this single assertion.
const prev = process.env.ANTHROPIC_API_KEY;
delete process.env.ANTHROPIC_API_KEY;
try {
const { db } = await import("../db");
const { users } = await import("../db/schema");
const { eq } = await import("drizzle-orm");
const uname = "stand2-" + Math.random().toString(36).slice(2, 10);
const [user] = await db
.insert(users)
.values({
username: uname,
email: `${uname}@example.com`,
passwordHash: "x",
})
.returning();
try {
const res = await generateStandup({
userId: user.id,
scope: "daily",
});
expect(typeof res.summary).toBe("string");
expect(res.summary.length).toBeGreaterThan(0);
expect(res.aiAvailable).toBe(false);
} finally {
try {
await db.delete(users).where(eq(users.id, user.id));
} catch {
/* ignore */
}
}
} finally {
if (prev) process.env.ANTHROPIC_API_KEY = prev;
}
}
);
});
|