all repos — sz-chrome-remote-cdp @ master

pi-browser-harness, patched to drive a Chrome on another host over CDP, with castle_cdp_search web search

test/profile/paths-test.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { homedir } from "node:os";
import { join } from "node:path";

const freshImport = async (): Promise<typeof import("../../src/profile/paths")> =>
  import(`../../src/profile/paths?cache=${Math.random()}`);

const withEnv = async (key: string, value: string | undefined, fn: () => Promise<void>): Promise<void> => {
  const saved = process.env[key];
  if (value === undefined) delete process.env[key];
  else process.env[key] = value;
  try {
    await fn();
  } finally {
    if (saved === undefined) delete process.env[key];
    else process.env[key] = saved;
  }
};

describe("path resolution", () => {
  test("A1: default agent dir is ~/.pi/agent", async () => {
    await withEnv("PI_CODING_AGENT_DIR", undefined, async () => {
      const { agentDir } = await freshImport();
      assert.equal(agentDir(), join(homedir(), ".pi", "agent"));
    });
  });

  test("A1: pin file sits in it", async () => {
    await withEnv("PI_CODING_AGENT_DIR", undefined, async () => {
      const { pinFilePath } = await freshImport();
      assert.equal(pinFilePath(), join(homedir(), ".pi", "agent", "browser-harness.json"));
    });
  });

  test("A1: '~/' in the agent-dir override expands like pi", async () => {
    await withEnv("PI_CODING_AGENT_DIR", "~/custom-agent", async () => {
      assert.equal((await freshImport()).agentDir(), `${homedir()}/custom-agent`);
    });
  });

  test("A1 win: '~\\' is left alone, matching pi's own resolution", { skip: process.platform !== "win32" }, async () => {
    await withEnv("PI_CODING_AGENT_DIR", "~\\custom-agent", async () => {
      assert.equal((await freshImport()).agentDir(), "~\\custom-agent");
    });
  });

  test("A2: candidates are non-empty on this platform", async () => {
    await withEnv("CHROME_USER_DATA_DIR", undefined, async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      assert.ok(dirs.length > 0);
    });
  });

  test("A2: candidate list is de-duplicated", async () => {
    await withEnv("CHROME_USER_DATA_DIR", undefined, async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      assert.equal(new Set(dirs).size, dirs.length);
    });
  });

  test("A2 linux: Chrome stable dir present", { skip: process.platform !== "linux" }, async () => {
    await withEnv("CHROME_USER_DATA_DIR", undefined, async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      assert.ok(dirs.includes(join(homedir(), ".config/google-chrome")));
    });
  });

  test("A2 linux: snap Chromium dir present", { skip: process.platform !== "linux" }, async () => {
    await withEnv("CHROME_USER_DATA_DIR", undefined, async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      assert.ok(dirs.includes(join(homedir(), "snap/chromium/common/chromium")));
    });
  });

  test("A2 linux: Brave dir present", { skip: process.platform !== "linux" }, async () => {
    await withEnv("CHROME_USER_DATA_DIR", undefined, async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      assert.ok(dirs.includes(join(homedir(), ".config/BraveSoftware/Brave-Browser")));
    });
  });

  test("A2 macos: Chrome dir present", { skip: process.platform !== "darwin" }, async () => {
    await withEnv("CHROME_USER_DATA_DIR", undefined, async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      const support = join(homedir(), "Library", "Application Support");
      assert.ok(dirs.includes(join(support, "Google/Chrome")));
    });
  });

  test("A2 macos: Brave dir present", { skip: process.platform !== "darwin" }, async () => {
    await withEnv("CHROME_USER_DATA_DIR", undefined, async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      const support = join(homedir(), "Library", "Application Support");
      assert.ok(dirs.includes(join(support, "BraveSoftware/Brave-Browser")));
    });
  });

  test("A2 macos: Edge dir present", { skip: process.platform !== "darwin" }, async () => {
    await withEnv("CHROME_USER_DATA_DIR", undefined, async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      const support = join(homedir(), "Library", "Application Support");
      assert.ok(dirs.includes(join(support, "Microsoft Edge")));
    });
  });

  test("A2 win: Chrome dir derives from %LOCALAPPDATA%", { skip: process.platform !== "win32" }, async () => {
    await withEnv("CHROME_USER_DATA_DIR", undefined, async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      const lad = process.env["LOCALAPPDATA"] ?? join(homedir(), "AppData", "Local");
      assert.ok(dirs.includes(join(lad, "Google/Chrome/User Data")));
    });
  });

  test("A2 win: Edge dir present", { skip: process.platform !== "win32" }, async () => {
    await withEnv("CHROME_USER_DATA_DIR", undefined, async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      const lad = process.env["LOCALAPPDATA"] ?? join(homedir(), "AppData", "Local");
      assert.ok(dirs.includes(join(lad, "Microsoft/Edge/User Data")));
    });
  });

  test("A3 win: redirected LOCALAPPDATA is used", { skip: process.platform !== "win32" }, async () => {
    await withEnv("LOCALAPPDATA", "D:\\Redirected\\Local", async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      assert.ok(dirs.some((d) => d.startsWith("D:\\Redirected\\Local") || d.startsWith("D:/Redirected/Local")));
    });
  });

  test("A4: env override is first and tilde-expanded", async () => {
    await withEnv("CHROME_USER_DATA_DIR", join("~", "my-chrome-data"), async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      assert.equal(dirs[0], join(homedir(), "my-chrome-data"));
    });
  });

  test("A4: forward-slash tilde expands on every platform", async () => {
    await withEnv("CHROME_USER_DATA_DIR", "~/my-chrome-data", async () => {
      const dirs = (await freshImport()).userDataDirCandidates();
      assert.equal(dirs[0], join(homedir(), "my-chrome-data"));
    });
  });

  test("A5: Chrome recognised", async () => {
    const { browserNameForUserDataDir } = await freshImport();
    assert.equal(browserNameForUserDataDir("/home/u/.config/google-chrome"), "Chrome");
  });

  test("A5: Brave recognised", async () => {
    const { browserNameForUserDataDir } = await freshImport();
    assert.equal(browserNameForUserDataDir("/home/u/.config/BraveSoftware/Brave-Browser"), "Brave");
  });

  test("A5: Edge recognised through Windows separators", async () => {
    const { browserNameForUserDataDir } = await freshImport();
    assert.equal(browserNameForUserDataDir("C:\\Users\\u\\AppData\\Local\\Microsoft\\Edge\\User Data"), "Edge");
  });

  test("A5: Chromium beats Chrome", async () => {
    const { browserNameForUserDataDir } = await freshImport();
    assert.equal(browserNameForUserDataDir("/home/u/.config/chromium"), "Chromium");
  });
});