示例工程 11-main-process-services

11-main-process-services/app/main/services/services.test.ts

typescript · 3.6 KB

import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { createDialogService } from "./dialog-service";
import { createNotesService } from "./notes-service";
import { createShellService } from "./shell-service";

let dir = "";

beforeEach(async () => {
  dir = await fs.mkdtemp(path.join(os.tmpdir(), "services-test-"));
});

afterEach(async () => {
  await fs.rm(dir, { recursive: true, force: true });
});

describe("notes-service", () => {
  it("写入后可读、可列,reset 清空", async () => {
    const notes = createNotesService({ dir });
    const meta = await notes.write("welcome", "# 你好");
    expect(meta.sizeBytes).toBeGreaterThan(0);
    expect((await notes.read("welcome")).content).toBe("# 你好");
    expect((await notes.list()).map((note) => note.id)).toEqual(["welcome"]);
    await notes.reset();
    expect(await notes.list()).toEqual([]);
  });

  it("拒绝非法 id,不产生文件", async () => {
    const notes = createNotesService({ dir });
    await expect(notes.write("../escape", "x")).rejects.toMatchObject({
      code: "BAD_ID",
    });
    expect(await fs.readdir(dir)).toEqual([]);
  });

  it("读取不存在的笔记返回 NOT_FOUND", async () => {
    const notes = createNotesService({ dir });
    await expect(notes.read("missing")).rejects.toMatchObject({
      code: "NOT_FOUND",
    });
  });

  it("写入使用临时文件 + rename,不留下 .tmp", async () => {
    const notes = createNotesService({ dir });
    await notes.write("atomic", "content");
    const files = await fs.readdir(dir);
    expect(files).toEqual(["atomic.md"]);
  });
});

describe("shell-service", () => {
  it("只允许 https + 白名单 host", async () => {
    const opened: string[] = [];
    const shell = createShellService({
      allowlist: ["example.com"],
      openExternal: async (url) => {
        opened.push(url);
      },
    });
    await expect(shell.open("https://example.com/docs")).resolves.toEqual({
      url: "https://example.com/docs",
      dryRun: false,
    });
    expect(opened).toEqual(["https://example.com/docs"]);
    await expect(shell.open("http://example.com")).rejects.toMatchObject({
      code: "URL_NOT_ALLOWED",
    });
    await expect(shell.open("https://evil.test")).rejects.toMatchObject({
      code: "URL_NOT_ALLOWED",
    });
  });

  it("dryRun 不调用 openExternal", async () => {
    let calls = 0;
    const shell = createShellService({
      allowlist: ["example.com"],
      openExternal: async () => {
        calls += 1;
      },
      dryRun: true,
    });
    await shell.open("https://example.com");
    expect(calls).toBe(0);
  });
});

describe("dialog-service", () => {
  it("返回选中的 .md 文件", async () => {
    const dialog = createDialogService({
      showOpenDialog: async () => ({
        canceled: false,
        filePaths: ["/tmp/notes/welcome.md"],
      }),
    });
    await expect(dialog.pick()).resolves.toEqual({
      path: "/tmp/notes/welcome.md",
      name: "welcome.md",
    });
  });

  it("取消返回 null", async () => {
    const dialog = createDialogService({
      showOpenDialog: async () => ({ canceled: true, filePaths: [] }),
    });
    await expect(dialog.pick()).resolves.toBeNull();
  });

  it("拒绝非 md/txt 扩展名", async () => {
    const dialog = createDialogService({
      showOpenDialog: async () => ({
        canceled: false,
        filePaths: ["/tmp/notes/app.exe"],
      }),
    });
    await expect(dialog.pick()).rejects.toMatchObject({
      code: "BAD_EXTENSION",
    });
  });
});