





















1+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
2+import { tmpdir } from "node:os";
3+import path from "node:path";
14import { afterEach, describe, expect, it, vi } from "vitest";
25import { resolveNextcloudTalkRoomKind, testing } from "./room-info.js";
3647const fetchWithSsrFGuard = vi.hoisted(() => vi.fn());
5-const readFileSync = vi.hoisted(() => vi.fn());
8+const tempDirs: string[] = [];
69710vi.mock("../runtime-api.js", () => {
8-return vi
9-.importActual<typeof import("../runtime-api.js")>("../runtime-api.js")
10-.then((actual) => ({
11- ...actual,
12- fetchWithSsrFGuard,
13-}));
14-});
15-16-vi.mock("node:fs", () => {
17-return vi.importActual<typeof import("node:fs")>("node:fs").then((actual) => ({
18- ...actual,
19- readFileSync,
20-}));
11+return { fetchWithSsrFGuard };
2112});
22132314afterEach(() => {
2415fetchWithSsrFGuard.mockReset();
25-readFileSync.mockReset();
2616testing.resetRoomCache();
17+for (const dir of tempDirs.splice(0)) {
18+rmSync(dir, { force: true, recursive: true });
19+}
2720});
282129-function requireFirstFetchParams(): { auditContext?: string; url?: string } {
22+function requireFirstFetchParams(): {
23+auditContext?: string;
24+init?: { headers?: { Authorization?: string } };
25+url?: string;
26+} {
3027const [call] = fetchWithSsrFGuard.mock.calls;
3128if (!call) {
3229throw new Error("expected Nextcloud Talk room info fetch call");
@@ -81,7 +78,10 @@ describe("nextcloud talk room info", () => {
8178const log = vi.fn();
8279const error = vi.fn();
8380const exit = vi.fn();
84-readFileSync.mockReturnValue("file-secret\n");
81+const tempDir = mkdtempSync(path.join(tmpdir(), "nextcloud-talk-room-info-"));
82+tempDirs.push(tempDir);
83+const passwordFile = path.join(tempDir, "secret");
84+writeFileSync(passwordFile, "file-secret\n", "utf-8");
8585fetchWithSsrFGuard.mockResolvedValue({
8686response: {
8787ok: false,
@@ -97,15 +97,17 @@ describe("nextcloud talk room info", () => {
9797baseUrl: "https://nc.example.com",
9898config: {
9999apiUser: "bot",
100-apiPasswordFile: "/tmp/nextcloud-secret",
100+apiPasswordFile: passwordFile,
101101},
102102} as never,
103103roomToken: "room-group",
104104runtime: { log, error, exit },
105105});
106106107107expect(kind).toBeUndefined();
108-expect(readFileSync).toHaveBeenCalledWith("/tmp/nextcloud-secret", "utf-8");
108+expect(requireFirstFetchParams().init?.headers?.Authorization).toBe(
109+"Basic Ym90OmZpbGUtc2VjcmV0",
110+);
109111expect(log).toHaveBeenCalledWith("nextcloud-talk: room lookup failed (403) token=room-group");
110112expect(release).toHaveBeenCalledTimes(1);
111113});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。