
























@@ -850,43 +850,62 @@ describe("gateway server misc", () => {
850850if (!configPath) {
851851throw new Error("Missing OPENCLAW_CONFIG_PATH");
852852}
853-await fs.mkdir(path.dirname(configPath), { recursive: true });
854-await fs.writeFile(
855-configPath,
856-JSON.stringify(
857-{
858-channels: {
859-discord: {
860-token: "token-123",
853+let previousConfig: string | undefined;
854+try {
855+previousConfig = await fs.readFile(configPath, "utf-8");
856+} catch (err) {
857+if ((err as NodeJS.ErrnoException).code !== "ENOENT") {
858+throw err;
859+}
860+}
861+862+try {
863+await fs.mkdir(path.dirname(configPath), { recursive: true });
864+await fs.writeFile(
865+configPath,
866+JSON.stringify(
867+{
868+channels: {
869+discord: {
870+token: "token-123",
871+},
861872},
862873},
863-},
864-null,
865-2,
866-),
867-"utf-8",
868-);
874+null,
875+2,
876+),
877+"utf-8",
878+);
869879870-await withEnvAsync(
871-{
872-OPENCLAW_TEST_MINIMAL_GATEWAY: undefined,
873-OPENCLAW_DISABLE_BUNDLED_PLUGINS: undefined,
874-OPENCLAW_BUNDLED_PLUGINS_DIR: path.resolve("extensions"),
875-},
876-async () => {
877-const autoPort = await getFreePort();
878-const autoServer = await startGatewayServer(autoPort);
879-await autoServer.close();
880-},
881-);
880+ await withEnvAsync(
881+ {
882+ OPENCLAW_TEST_MINIMAL_GATEWAY: undefined,
883+ OPENCLAW_DISABLE_BUNDLED_PLUGINS: undefined,
884+ OPENCLAW_BUNDLED_PLUGINS_DIR: path.resolve("extensions"),
885+ },
886+ async () => {
887+ const autoPort = await getFreePort();
888+ const autoServer = await startGatewayServer(autoPort);
889+ await autoServer.close();
890+ },
891+ );
882892883-const updated = JSON.parse(await fs.readFile(configPath, "utf-8")) as Record<string, unknown>;
884-const channels = updated.channels as Record<string, unknown> | undefined;
885-const discord = channels?.discord as Record<string, unknown> | undefined;
886-expect(discord).toMatchObject({
887-token: "token-123",
888-enabled: true,
889-});
893+const updated = JSON.parse(await fs.readFile(configPath, "utf-8")) as Record<string, unknown>;
894+const channels = updated.channels as Record<string, unknown> | undefined;
895+const discord = channels?.discord as Record<string, unknown> | undefined;
896+expect(discord).toMatchObject({
897+token: "token-123",
898+enabled: true,
899+});
900+} finally {
901+if (previousConfig === undefined) {
902+await fs.rm(configPath, { force: true });
903+} else {
904+await fs.writeFile(configPath, previousConfig, "utf-8");
905+}
906+clearRuntimeConfigSnapshot();
907+clearConfigCache();
908+}
890909});
891910892911test("releases port after close", async () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。