test: debrand generic setup helper fixtures

This commit is contained in:
Peter Steinberger
2026-03-27 21:57:38 +00:00
parent 5f7f914796
commit 884247f8d8
2 changed files with 57 additions and 42 deletions

View File

@@ -18,18 +18,18 @@ describe("applySetupAccountConfigPatch", () => {
const next = applySetupAccountConfigPatch({
cfg: asConfig({
channels: {
zalo: {
"demo-setup": {
webhookPath: "/old",
enabled: false,
},
},
}),
channelKey: "zalo",
channelKey: "demo-setup",
accountId: DEFAULT_ACCOUNT_ID,
patch: { webhookPath: "/new", botToken: "tok" },
});
expect(next.channels?.zalo).toMatchObject({
expect(next.channels?.["demo-setup"]).toMatchObject({
enabled: true,
webhookPath: "/new",
botToken: "tok",
@@ -40,7 +40,7 @@ describe("applySetupAccountConfigPatch", () => {
const next = applySetupAccountConfigPatch({
cfg: asConfig({
channels: {
zalo: {
"demo-setup": {
enabled: false,
accounts: {
work: { botToken: "old", enabled: false },
@@ -48,12 +48,12 @@ describe("applySetupAccountConfigPatch", () => {
},
},
}),
channelKey: "zalo",
channelKey: "demo-setup",
accountId: "work",
patch: { botToken: "new" },
});
expect(next.channels?.zalo).toMatchObject({
expect(next.channels?.["demo-setup"]).toMatchObject({
enabled: true,
accounts: {
work: { enabled: false, botToken: "new" },
@@ -65,19 +65,19 @@ describe("applySetupAccountConfigPatch", () => {
const next = applySetupAccountConfigPatch({
cfg: asConfig({
channels: {
zalo: {
"demo-setup": {
accounts: {
personal: { botToken: "personal-token" },
},
},
},
}),
channelKey: "zalo",
channelKey: "demo-setup",
accountId: "Work Team",
patch: { botToken: "work-token" },
});
expect(next.channels?.zalo).toMatchObject({
expect(next.channels?.["demo-setup"]).toMatchObject({
accounts: {
personal: { botToken: "personal-token" },
"work-team": { enabled: true, botToken: "work-token" },
@@ -89,17 +89,17 @@ describe("applySetupAccountConfigPatch", () => {
describe("createPatchedAccountSetupAdapter", () => {
it("stores default-account patch at channel root", () => {
const adapter = createPatchedAccountSetupAdapter({
channelKey: "zalo",
channelKey: "demo-setup",
buildPatch: (input) => ({ botToken: input.token }),
});
const next = adapter.applyAccountConfig({
cfg: asConfig({ channels: { zalo: { enabled: false } } }),
cfg: asConfig({ channels: { "demo-setup": { enabled: false } } }),
accountId: DEFAULT_ACCOUNT_ID,
input: { name: "Personal", token: "tok" },
});
expect(next.channels?.zalo).toMatchObject({
expect(next.channels?.["demo-setup"]).toMatchObject({
enabled: true,
name: "Personal",
botToken: "tok",
@@ -108,14 +108,14 @@ describe("createPatchedAccountSetupAdapter", () => {
it("migrates base name into the default account before patching a named account", () => {
const adapter = createPatchedAccountSetupAdapter({
channelKey: "zalo",
channelKey: "demo-setup",
buildPatch: (input) => ({ botToken: input.token }),
});
const next = adapter.applyAccountConfig({
cfg: asConfig({
channels: {
zalo: {
"demo-setup": {
name: "Personal",
accounts: {
work: { botToken: "old" },
@@ -127,30 +127,30 @@ describe("createPatchedAccountSetupAdapter", () => {
input: { name: "Work", token: "new" },
});
expect(next.channels?.zalo).toMatchObject({
expect(next.channels?.["demo-setup"]).toMatchObject({
accounts: {
default: { name: "Personal" },
work: { botToken: "old" },
"work-team": { enabled: true, name: "Work", botToken: "new" },
},
});
expect(next.channels?.zalo).not.toHaveProperty("name");
expect(next.channels?.["demo-setup"]).not.toHaveProperty("name");
});
it("can store the default account in accounts.default", () => {
const adapter = createPatchedAccountSetupAdapter({
channelKey: "whatsapp",
channelKey: "demo-accounts",
alwaysUseAccounts: true,
buildPatch: (input) => ({ authDir: input.authDir }),
});
const next = adapter.applyAccountConfig({
cfg: asConfig({ channels: { whatsapp: {} } }),
cfg: asConfig({ channels: { "demo-accounts": {} } }),
accountId: DEFAULT_ACCOUNT_ID,
input: { name: "Phone", authDir: "/tmp/auth" },
});
expect(next.channels?.whatsapp).toMatchObject({
expect(next.channels?.["demo-accounts"]).toMatchObject({
accounts: {
default: {
enabled: true,
@@ -159,8 +159,8 @@ describe("createPatchedAccountSetupAdapter", () => {
},
},
});
expect(next.channels?.whatsapp).not.toHaveProperty("enabled");
expect(next.channels?.whatsapp).not.toHaveProperty("authDir");
expect(next.channels?.["demo-accounts"]).not.toHaveProperty("enabled");
expect(next.channels?.["demo-accounts"]).not.toHaveProperty("authDir");
});
});
@@ -270,7 +270,7 @@ describe("moveSingleAccountChannelSectionToDefaultAccount", () => {
describe("createEnvPatchedAccountSetupAdapter", () => {
it("rejects env mode for named accounts and requires credentials otherwise", () => {
const adapter = createEnvPatchedAccountSetupAdapter({
channelKey: "telegram",
channelKey: "demo-env",
defaultAccountOnlyEnvError: "env only on default",
missingCredentialError: "token required",
hasCredentials: (input) => Boolean(input.token || input.tokenFile),
@@ -308,35 +308,35 @@ describe("prepareScopedSetupConfig", () => {
const next = prepareScopedSetupConfig({
cfg: asConfig({
channels: {
bluebubbles: {
"demo-scoped": {
name: "Personal",
},
},
}),
channelKey: "bluebubbles",
channelKey: "demo-scoped",
accountId: "Work Team",
name: "Work",
migrateBaseName: true,
});
expect(next.channels?.bluebubbles).toMatchObject({
expect(next.channels?.["demo-scoped"]).toMatchObject({
accounts: {
default: { name: "Personal" },
"work-team": { name: "Work" },
},
});
expect(next.channels?.bluebubbles).not.toHaveProperty("name");
expect(next.channels?.["demo-scoped"]).not.toHaveProperty("name");
});
it("keeps the base shape for the default account when migration is disabled", () => {
const next = prepareScopedSetupConfig({
cfg: asConfig({ channels: { irc: { enabled: true } } }),
channelKey: "irc",
cfg: asConfig({ channels: { "demo-base": { enabled: true } } }),
channelKey: "demo-base",
accountId: DEFAULT_ACCOUNT_ID,
name: "Libera",
});
expect(next.channels?.irc).toMatchObject({
expect(next.channels?.["demo-base"]).toMatchObject({
enabled: true,
name: "Libera",
});

View File

@@ -19,8 +19,14 @@ describe("delivery-queue recovery", () => {
const baseCfg = {};
const enqueueCrashRecoveryEntries = async () => {
await enqueueDelivery({ channel: "whatsapp", to: "+1", payloads: [{ text: "a" }] }, tmpDir());
await enqueueDelivery({ channel: "telegram", to: "2", payloads: [{ text: "b" }] }, tmpDir());
await enqueueDelivery(
{ channel: "demo-channel-a", to: "+1", payloads: [{ text: "a" }] },
tmpDir(),
);
await enqueueDelivery(
{ channel: "demo-channel-b", to: "2", payloads: [{ text: "b" }] },
tmpDir(),
);
};
const runRecovery = async ({
@@ -60,7 +66,7 @@ describe("delivery-queue recovery", () => {
it("moves entries that exceeded max retries to failed/", async () => {
const id = await enqueueDelivery(
{ channel: "whatsapp", to: "+1", payloads: [{ text: "a" }] },
{ channel: "demo-channel-a", to: "+1", payloads: [{ text: "a" }] },
tmpDir(),
);
setQueuedEntryState(tmpDir(), id, { retryCount: MAX_RETRIES });
@@ -75,7 +81,10 @@ describe("delivery-queue recovery", () => {
});
it("increments retryCount on failed recovery attempt", async () => {
await enqueueDelivery({ channel: "slack", to: "#ch", payloads: [{ text: "x" }] }, tmpDir());
await enqueueDelivery(
{ channel: "demo-channel-c", to: "#ch", payloads: [{ text: "x" }] },
tmpDir(),
);
const deliver = vi.fn().mockRejectedValue(new Error("network down"));
const { result } = await runRecovery({ deliver });
@@ -91,7 +100,7 @@ describe("delivery-queue recovery", () => {
it("moves entries to failed/ immediately on permanent delivery errors", async () => {
const id = await enqueueDelivery(
{ channel: "msteams", to: "user:abc", payloads: [{ text: "hi" }] },
{ channel: "demo-channel", to: "user:abc", payloads: [{ text: "hi" }] },
tmpDir(),
);
const deliver = vi
@@ -108,7 +117,10 @@ describe("delivery-queue recovery", () => {
});
it("passes skipQueue: true to prevent re-enqueueing during recovery", async () => {
await enqueueDelivery({ channel: "whatsapp", to: "+1", payloads: [{ text: "a" }] }, tmpDir());
await enqueueDelivery(
{ channel: "demo-channel-a", to: "+1", payloads: [{ text: "a" }] },
tmpDir(),
);
const deliver = vi.fn().mockResolvedValue([]);
await runRecovery({ deliver });
@@ -119,7 +131,7 @@ describe("delivery-queue recovery", () => {
it("replays stored delivery options during recovery", async () => {
await enqueueDelivery(
{
channel: "whatsapp",
channel: "demo-channel-a",
to: "+1",
payloads: [{ text: "a" }],
bestEffort: true,
@@ -155,7 +167,10 @@ describe("delivery-queue recovery", () => {
it("respects maxRecoveryMs time budget and bumps deferred retries", async () => {
await enqueueCrashRecoveryEntries();
await enqueueDelivery({ channel: "slack", to: "#c", payloads: [{ text: "c" }] }, tmpDir());
await enqueueDelivery(
{ channel: "demo-channel-c", to: "#c", payloads: [{ text: "c" }] },
tmpDir(),
);
const deliver = vi.fn().mockResolvedValue([]);
const { result, log } = await runRecovery({
@@ -179,7 +194,7 @@ describe("delivery-queue recovery", () => {
it("defers entries until backoff becomes eligible", async () => {
const id = await enqueueDelivery(
{ channel: "whatsapp", to: "+1", payloads: [{ text: "a" }] },
{ channel: "demo-channel-a", to: "+1", payloads: [{ text: "a" }] },
tmpDir(),
);
setQueuedEntryState(tmpDir(), id, { retryCount: 3, lastAttemptAt: Date.now() });
@@ -204,11 +219,11 @@ describe("delivery-queue recovery", () => {
it("continues past high-backoff entries and recovers ready entries behind them", async () => {
const now = Date.now();
const blockedId = await enqueueDelivery(
{ channel: "whatsapp", to: "+1", payloads: [{ text: "blocked" }] },
{ channel: "demo-channel-a", to: "+1", payloads: [{ text: "blocked" }] },
tmpDir(),
);
const readyId = await enqueueDelivery(
{ channel: "telegram", to: "2", payloads: [{ text: "ready" }] },
{ channel: "demo-channel-b", to: "2", payloads: [{ text: "ready" }] },
tmpDir(),
);
@@ -230,7 +245,7 @@ describe("delivery-queue recovery", () => {
});
expect(deliver).toHaveBeenCalledTimes(1);
expect(deliver).toHaveBeenCalledWith(
expect.objectContaining({ channel: "telegram", to: "2", skipQueue: true }),
expect.objectContaining({ channel: "demo-channel-b", to: "2", skipQueue: true }),
);
const remaining = await loadPendingDeliveries(tmpDir());
@@ -244,7 +259,7 @@ describe("delivery-queue recovery", () => {
vi.setSystemTime(start);
const id = await enqueueDelivery(
{ channel: "whatsapp", to: "+1", payloads: [{ text: "later" }] },
{ channel: "demo-channel-a", to: "+1", payloads: [{ text: "later" }] },
tmpDir(),
);
setQueuedEntryState(tmpDir(), id, { retryCount: 3, lastAttemptAt: start.getTime() });