Skip to content
66 changes: 48 additions & 18 deletions test/e2e/live/channels-add-remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ async function expectProvider(
}
}

async function openClawHasTelegram(sandbox: SandboxClient, artifactName: string): Promise<boolean> {
/** Read non-secret Telegram activation state from the rendered OpenClaw config. */
async function readOpenClawTelegramState(
sandbox: SandboxClient,
artifactName: string,
): Promise<JsonRecord> {
const result = await sandbox.exec(
SANDBOX_NAME,
[
Expand All @@ -291,7 +295,13 @@ async function openClawHasTelegram(sandbox: SandboxClient, artifactName: string)
[
"import json",
"data=json.load(open('/sandbox/.openclaw/openclaw.json'))",
"print('yes' if 'telegram' in data.get('channels', {}) else 'no')",
"channels=data.get('channels', {})",
"plugins=data.get('plugins', {}).get('entries', {})",
"channel=channels.get('telegram', {})",
"plugin=plugins.get('telegram', {})",
"accounts=channel.get('accounts', {})",
"state={'channelPresent': 'telegram' in channels, 'pluginPresent': 'telegram' in plugins, 'channelEnabled': channel.get('enabled') is True, 'pluginEnabled': plugin.get('enabled') is True, 'accountEnabled': any(isinstance(account, dict) and account.get('enabled') is True for account in accounts.values())}",
"print(json.dumps(state))",
].join("; "),
],
{
Expand All @@ -301,21 +311,11 @@ async function openClawHasTelegram(sandbox: SandboxClient, artifactName: string)
},
);
assertExitZero(result, "read /sandbox/.openclaw/openclaw.json");
const verdict = stripAnsi(result.stdout).trim().split(/\r?\n/).at(-1);
expect(["yes", "no"], `unexpected openclaw.json verdict:\n${resultText(result)}`).toContain(
verdict,
);
return verdict === "yes";
}

async function expectOpenClawTelegram(
sandbox: SandboxClient,
expected: boolean,
artifactName: string,
): Promise<void> {
await expect(openClawHasTelegram(sandbox, artifactName)).resolves.toBe(expected);
const output = stripAnsi(result.stdout).trim().split(/\r?\n/).at(-1) ?? "";
return JSON.parse(output) as JsonRecord;
}

/** Detect an active policy preset in the human-readable policy listing. */
function policyListHasActivePreset(output: string, preset: string): boolean {
const activePreset = new RegExp(`^\\s*\\u25cf\\s+${escapeRegex(preset)}\\b`, "im");
return activePreset.test(stripAnsi(output));
Expand Down Expand Up @@ -465,7 +465,17 @@ test(

progress.phase("verify baseline channel absence");
await expectProvider(host, "absent", "phase-2-provider-get-baseline");
await expectOpenClawTelegram(sandbox, false, "phase-2-openclaw-json-baseline");
const baselineTelegram = await readOpenClawTelegramState(
sandbox,
"phase-2-openclaw-json-baseline",
);
expect(baselineTelegram).toEqual({
accountEnabled: false,
channelEnabled: false,
channelPresent: true,
pluginEnabled: false,
pluginPresent: false,
});
await expectPolicyPreset(host, "telegram", "not-applied", "phase-2-policy-list-baseline");

progress.phase("add Telegram and rebuild sandbox");
Expand Down Expand Up @@ -507,7 +517,17 @@ test(

progress.phase("validate active Telegram integration");
await expectPolicyPreset(host, "telegram", "applied", "phase-4-policy-list-after-add");
await expectOpenClawTelegram(sandbox, true, "phase-4-openclaw-json-after-add");
const activeTelegram = await readOpenClawTelegramState(
sandbox,
"phase-4-openclaw-json-after-add",
);
expect(activeTelegram).toEqual({
accountEnabled: true,
channelEnabled: true,
channelPresent: true,
pluginEnabled: true,
pluginPresent: true,
});
await expectProvider(host, "present", "phase-4-provider-get-after-add");
expectHostTelegramConfig("after add+rebuild");
expectHostTelegramPlan("active", "after add+rebuild");
Expand Down Expand Up @@ -551,7 +571,17 @@ test(
});

progress.phase("validate Telegram removal");
await expectOpenClawTelegram(sandbox, false, "phase-6-openclaw-json-after-remove");
const removedTelegram = await readOpenClawTelegramState(
sandbox,
"phase-6-openclaw-json-after-remove",
);
expect(removedTelegram).toEqual({
accountEnabled: false,
channelEnabled: false,
channelPresent: false,
pluginEnabled: false,
pluginPresent: false,
});
await expectProvider(host, "absent", "phase-6-provider-get-after-remove");
await expectPolicyPreset(host, "telegram", "not-applied", "phase-6-policy-list-after-remove");
expectHostTelegramPlan("removed", "after remove+rebuild");
Expand Down
Loading