|
1 | 1 | /** Tests Discord secret surfaces in runtime preparation. */ |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
2 | 4 | import { describe, expect, it } from "vitest"; |
| 5 | +import { withTempDir } from "../test-helpers/temp-dir.js"; |
3 | 6 | import "./runtime-discord.test-support.ts"; |
4 | 7 | import { |
5 | 8 | asConfig, |
@@ -71,6 +74,103 @@ describe("secrets runtime snapshot discord surface", () => { |
71 | 74 | ); |
72 | 75 | }); |
73 | 76 |
|
| 77 | + it.skipIf(process.platform === "win32")( |
| 78 | + "resolves the implicit default token when named Discord accounts are added", |
| 79 | + async () => { |
| 80 | + await withTempDir({ prefix: "openclaw-discord-secrets-" }, async (root) => { |
| 81 | + const secretsPath = path.join(root, "secrets.json"); |
| 82 | + await fs.writeFile( |
| 83 | + secretsPath, |
| 84 | + JSON.stringify({ |
| 85 | + discord: { |
| 86 | + defaultToken: "default-account-token", |
| 87 | + secondToken: "second-account-token", |
| 88 | + }, |
| 89 | + }), |
| 90 | + "utf8", |
| 91 | + ); |
| 92 | + await fs.chmod(secretsPath, 0o600); |
| 93 | + |
| 94 | + const snapshot = await prepareSecretsRuntimeSnapshot({ |
| 95 | + config: asConfig({ |
| 96 | + secrets: { |
| 97 | + providers: { |
| 98 | + discord_file: { |
| 99 | + source: "file", |
| 100 | + path: secretsPath, |
| 101 | + mode: "json", |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + channels: { |
| 106 | + discord: { |
| 107 | + token: { |
| 108 | + source: "file", |
| 109 | + provider: "discord_file", |
| 110 | + id: "/discord/defaultToken", |
| 111 | + }, |
| 112 | + accounts: { |
| 113 | + second: { |
| 114 | + enabled: true, |
| 115 | + token: { |
| 116 | + source: "file", |
| 117 | + provider: "discord_file", |
| 118 | + id: "/discord/secondToken", |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }), |
| 125 | + agentDirs: ["/tmp/openclaw-agent-main"], |
| 126 | + loadAuthStore: () => loadAuthStoreWithProfiles({}), |
| 127 | + }); |
| 128 | + |
| 129 | + expect(snapshot.config.channels?.discord?.token).toBe("default-account-token"); |
| 130 | + expect(snapshot.config.channels?.discord?.accounts?.second?.token).toBe( |
| 131 | + "second-account-token", |
| 132 | + ); |
| 133 | + expect(snapshot.warnings.map((warning) => warning.path)).not.toContain( |
| 134 | + "channels.discord.token", |
| 135 | + ); |
| 136 | + }); |
| 137 | + }, |
| 138 | + ); |
| 139 | + |
| 140 | + it("keeps inherited refs active for an env-backed implicit default", async () => { |
| 141 | + const snapshot = await prepareSecretsRuntimeSnapshot({ |
| 142 | + config: asConfig({ |
| 143 | + channels: { |
| 144 | + discord: { |
| 145 | + pluralkit: { |
| 146 | + token: { source: "env", provider: "default", id: "DISCORD_DEFAULT_PK_TOKEN" }, |
| 147 | + }, |
| 148 | + accounts: { |
| 149 | + second: { |
| 150 | + pluralkit: { |
| 151 | + token: { source: "env", provider: "default", id: "DISCORD_SECOND_PK_TOKEN" }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }), |
| 158 | + env: { |
| 159 | + DISCORD_BOT_TOKEN: "env-default-token", |
| 160 | + DISCORD_DEFAULT_PK_TOKEN: "default-pk-token", |
| 161 | + DISCORD_SECOND_PK_TOKEN: "second-pk-token", |
| 162 | + }, |
| 163 | + agentDirs: ["/tmp/openclaw-agent-main"], |
| 164 | + loadAuthStore: () => loadAuthStoreWithProfiles({}), |
| 165 | + }); |
| 166 | + |
| 167 | + expect(snapshot.config.channels?.discord?.pluralkit?.token).toBe("default-pk-token"); |
| 168 | + expect(snapshot.config.channels?.discord?.accounts?.second?.pluralkit?.token).toBe( |
| 169 | + "second-pk-token", |
| 170 | + ); |
| 171 | + expect(snapshot.warnings).toStrictEqual([]); |
| 172 | + }); |
| 173 | + |
74 | 174 | it("fails when non-default Discord account inherits an unresolved top-level token ref", async () => { |
75 | 175 | await expect( |
76 | 176 | prepareSecretsRuntimeSnapshot({ |
|
0 commit comments