117 lines
6.6 KiB
TypeScript
117 lines
6.6 KiB
TypeScript
// PEP-304: subscriptions in a limit are not «no subscription»; the time people read is in the
|
||
// platform's zone (PEPPA_TZ), whatever the process's own zone is.
|
||
import assert from "node:assert/strict";
|
||
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
||
import { tmpdir } from "node:os";
|
||
import { join, resolve } from "node:path";
|
||
import { fileURLToPath } from "node:url";
|
||
import { after, before, describe, test } from "node:test";
|
||
import { platformDayTime, platformTime, platformZone } from "@peppa/shared/time";
|
||
import { stripAsIs } from "@peppa/shared/i18n";
|
||
import { releases } from "@peppa/db";
|
||
import { core, db, runner, teardown } from "./helpers.ts";
|
||
|
||
process.env.PEPPA_SECRET = "mock-secret-for-tests";
|
||
const ROOT = resolve(fileURLToPath(import.meta.url), "../../../..");
|
||
const TMP = mkdtempSync(join(tmpdir(), "peppa-limit-reason-"));
|
||
const scenario = join(TMP, "scenario.json");
|
||
const savedTz = process.env.PEPPA_TZ;
|
||
|
||
type User = Awaited<ReturnType<typeof core.registerEgovUser>>;
|
||
let owner: User;
|
||
let seq = 0;
|
||
before(async () => {
|
||
process.env.PEPPA_INTAKE_BIN = join(ROOT, "packages/testkit/bin/mock-intake");
|
||
process.env.MOCK_INTAKE_SCENARIO = scenario;
|
||
writeFileSync(scenario, "{}");
|
||
// Self-registered: no machine login for its projects — only the project's own subscriptions.
|
||
owner = await core.registerEgovUser(db, { iin: "900101300304", name: "САДОВА ГУЛЬНАРА" });
|
||
});
|
||
after(async () => {
|
||
if (savedTz === undefined) delete process.env.PEPPA_TZ;
|
||
else process.env.PEPPA_TZ = savedTz;
|
||
await teardown();
|
||
});
|
||
|
||
async function project(withSub: "limited" | "none") {
|
||
const n = ++seq;
|
||
const ws = join(TMP, `ws${n}`);
|
||
mkdirSync(join(ws, ".peppa"), { recursive: true });
|
||
const p = await core.createProject(db, { slug: `mock-garden-${n}`, key: `GRD${String.fromCharCode(64 + n)}`, name: `Сад ${n}`, giteaOwner: "x", workspacePath: ws, ownerId: owner.id });
|
||
await core.setIntakeSettings(db, p.id, { enabled: true });
|
||
if (withSub === "limited") {
|
||
const sub = await core.createSubscription(db, { provider: "claude", name: `mock-sub-${p.key}`, authKind: "oauth", secret: "mock-token" });
|
||
await core.updateSubscription(db, sub.id, { status: "limited", limitedUntil: RESET });
|
||
const pool = await core.createPool(db, { name: `mock-pool-${p.key}`, subscriptionIds: [sub.id] });
|
||
await core.setProjectPool(db, p.id, pool.id);
|
||
}
|
||
return p;
|
||
}
|
||
// A future reset (the subscriptions judge «limited» by the real clock); 18:50 UTC = 23:50 in Almaty.
|
||
const RESET = new Date("2030-01-10T18:50:00Z");
|
||
const notes = async (taskId: number) => (await core.storyComments(db, taskId)).map((c) => stripAsIs(c.text));
|
||
|
||
describe("PEP-304: подписки в лимите — не «нет подписки»", () => {
|
||
test("Scenario: Все подписки в лимите — заметка с временем сброса, разбор повторяется после него", async () => {
|
||
process.env.PEPPA_TZ = "Asia/Almaty"; // UTC+5
|
||
const p = await project("limited");
|
||
const ref = await core.createTask(db, { projectId: p.id, title: "Полив по расписанию", description: "Хочу включать полив по часам" });
|
||
const now = new Date("2030-01-10T18:35:00Z");
|
||
await runner.intakeTick(db, { wait: true, now });
|
||
await runner.intakeTick(db, { wait: true, now: new Date(now.getTime() + 60_000) });
|
||
let state = (await core.getIntakeState(db, ref.task.id))!;
|
||
const said = await notes(ref.task.id);
|
||
assert.equal(said.length, 1, "one note per limit");
|
||
assert.match(said[0]!, /подписки проекта в лимите до 23:50/);
|
||
assert.doesNotMatch(said[0]!, /не подключена подписка|идёт в работу как написана/);
|
||
assert.notEqual(state.stage, "done", "the story waits for the analyst, not «as written»");
|
||
assert.equal(state.attempts, 0, "a limit is no attempt");
|
||
assert.equal(state.retryAt, Date.parse("2030-01-10T18:51:00Z"));
|
||
|
||
// Before the reset: not even a try. After it the subscription is usable again — the analyst goes through the story.
|
||
await runner.intakeTick(db, { wait: true, now: new Date("2030-01-10T18:50:30Z") });
|
||
assert.notEqual((await core.getIntakeState(db, ref.task.id))!.stage, "done");
|
||
const [sub] = (await core.listSubscriptions(db)).filter((x) => x.name === `mock-sub-${p.key}`);
|
||
await core.updateSubscription(db, sub!.id, { status: "active", limitedUntil: null });
|
||
await runner.intakeTick(db, { wait: true, now: new Date("2030-01-10T18:52:00Z") });
|
||
state = (await core.getIntakeState(db, ref.task.id))!;
|
||
assert.equal(state.stage, "done");
|
||
assert.equal((await notes(ref.task.id)).filter((t) => /в лимите/.test(t)).length, 1);
|
||
});
|
||
|
||
test("Scenario: Подписки нет — прежняя причина «не подключена подписка»", async () => {
|
||
const p = await project("none");
|
||
const ref = await core.createTask(db, { projectId: p.id, title: "Учёт урожая", description: "Записывать, сколько собрали" });
|
||
await runner.intakeTick(db, { wait: true });
|
||
await runner.intakeTick(db, { wait: true });
|
||
const said = await notes(ref.task.id);
|
||
assert.equal(said.length, 1);
|
||
assert.match(said[0]!, /у проекта не подключена подписка/);
|
||
});
|
||
});
|
||
|
||
describe("PEP-304: время для людей — в поясе платформы", () => {
|
||
test("Scenario: Пояс платформы отличается от пояса сервера", () => {
|
||
process.env.PEPPA_TZ = "Asia/Almaty";
|
||
const at = new Date("2026-11-25T18:35:00Z");
|
||
assert.equal(platformTime(at), "23:35");
|
||
assert.equal(platformDayTime(at), "25.11, 23:35");
|
||
process.env.PEPPA_TZ = "America/New_York";
|
||
assert.equal(platformTime(at), "13:35");
|
||
});
|
||
|
||
test("Scenario: Неверный пояс — время в поясе процесса, без падения", () => {
|
||
process.env.PEPPA_TZ = "Not/AZone";
|
||
assert.equal(platformZone(), undefined);
|
||
const at = new Date("2026-11-25T18:35:00Z");
|
||
assert.equal(platformTime(at), at.toLocaleTimeString("ru-RU", { hour: "2-digit", minute: "2-digit", hourCycle: "h23" }));
|
||
});
|
||
|
||
test("Scenario: Подпись версии в чате — в поясе платформы", async () => {
|
||
process.env.PEPPA_TZ = "Asia/Almaty";
|
||
const p = await project("none");
|
||
const [rel] = await db.insert(releases).values({ projectId: p.id, env: "staging", sha: "a".repeat(40), status: "healthy", createdAt: new Date("2026-11-25T18:35:00Z") }).returning();
|
||
assert.match(stripAsIs((await core.threadTitle(db, `release:${rel!.id}`))!), /25\.11, 23:35$/);
|
||
});
|
||
});
|