Files
seal-use/src/core/auth/session-fallback.test.ts
T
gitea 8c9f102931 feat: 原生 Seal 授权续期自愈与本地 SQLite 分歧待办闭环
- 授权自愈: 引入长效会话 Cookie 备份池,支持 Seal 原生协助授权到期与 401 报错时平滑自动换发与静默续期;
- 存储升级: 审核报告人工裁决底座迁移至 SQLite (~/.config/seal-use/state/reviews.sqlite),支持历史 JSONL 自动平滑导入与向后兼容双写;
- 分歧待办: 新增 report review unreviewed 与 dismiss 命令,收集人机不一致单据并在日常企业命令完成后于 stderr 注入 Agent 动作指引;
- 精密频控: 实现 30 天单据时效窗口过滤(陈年单据自动归档)、动态 5 小时滑动窗口频控与 24 小时人工拒审静音抑制协议;
- 透明认知: 全面补充 Skill 路由与帮助手册,消除黑盒,透传数据边界与安全红线。
2026-11-02 15:22:47 +08:00

96 lines
3.2 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import {
attemptCreateDelegationWithFallback,
resolveCandidateCookies,
rotateBackupCookies
} from "./session-fallback.js";
describe("session-fallback", () => {
test("rotateBackupCookies: 初始无历史 Cookie 时返回空备份", () => {
const result = rotateBackupCookies(undefined, "cookie-1");
expect(result).toEqual([]);
});
test("rotateBackupCookies: 更新 Cookie 时自动归档上一份会话且排除当前 Cookie", () => {
const existing = { cookie: "cookie-1", backupCookies: [] };
const result = rotateBackupCookies(existing, "cookie-2");
expect(result).toEqual(["cookie-1"]);
});
test("rotateBackupCookies: 连续轮换自动去重并严格限制上限为 5 份", () => {
let existing: { cookie?: string; backupCookies?: string[] } = {
cookie: "cookie-1",
backupCookies: []
};
for (let i = 2; i <= 8; i++) {
const newCookie = `cookie-${i}`;
const backups = rotateBackupCookies(existing, newCookie);
existing = { cookie: newCookie, backupCookies: backups };
}
expect(existing.cookie).toBe("cookie-8");
expect(existing.backupCookies?.length).toBe(5);
expect(existing.backupCookies).toEqual([
"cookie-7",
"cookie-6",
"cookie-5",
"cookie-4",
"cookie-3"
]);
});
test("resolveCandidateCookies: 优先返回当前主 Cookie 并依次排列历史长效备份", () => {
expect(resolveCandidateCookies(undefined)).toEqual([]);
expect(resolveCandidateCookies({})).toEqual([]);
const result = resolveCandidateCookies({
cookie: "main-cookie",
backupCookies: ["backup-1", "backup-2", "main-cookie"]
});
expect(result).toEqual(["main-cookie", "backup-1", "backup-2"]);
});
test("attemptCreateDelegationWithFallback: 主 Cookie 成功时直接返回结果", async () => {
const tried: (string | undefined)[] = [];
const res = await attemptCreateDelegationWithFallback(
["cookie-primary", "cookie-backup-1"],
async (cookie) => {
tried.push(cookie);
return { link: "https://seal.cc/delegate?token=new", token: "new-token" };
}
);
expect(res.token).toBe("new-token");
expect(tried).toEqual(["cookie-primary"]);
});
test("attemptCreateDelegationWithFallback: 主 Cookie 失败时依次由备份 Cookie 兜底救险", async () => {
const tried: (string | undefined)[] = [];
const res = await attemptCreateDelegationWithFallback(
["expired-cookie", "valid-backup-cookie"],
async (cookie) => {
tried.push(cookie);
if (cookie === "expired-cookie") {
throw new Error("401 Unauthorized");
}
return { link: "https://seal.cc/delegate?token=rescued", token: "rescued-token" };
}
);
expect(res.token).toBe("rescued-token");
expect(tried).toEqual(["expired-cookie", "valid-backup-cookie"]);
});
test("attemptCreateDelegationWithFallback: 全部候选失败时抛出最后的错误", async () => {
await expect(
attemptCreateDelegationWithFallback(
["bad-1", "bad-2"],
async (cookie) => {
throw new Error(`Failed with ${cookie}`);
}
)
).rejects.toThrow("Failed with bad-2");
});
});