File size: 1,476 Bytes
476094d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";

import { migrateCodexAccountPool } from "./migrate-codex-acc-pool.mjs";

async function makeTempTokensDir() {
  return fs.mkdtemp(path.join(os.tmpdir(), "migrate-codex-pool-test-"));
}

test("migrateCodexAccountPool merges scattered account files into pool.json and backs them up", async () => {
  const dir = await makeTempTokensDir();
  await fs.writeFile(
    path.join(dir, "a.json"),
    JSON.stringify({
      type: "codex",
      email: "a@example.com",
      tokens: {
        access_token: "token-a",
        account_id: "acc-a",
        id_token: "id-a",
        refresh_token: "rt-a",
      },
    }),
  );
  await fs.writeFile(
    path.join(dir, "b.json"),
    JSON.stringify({
      type: "codex",
      email: "b@example.com",
      tokens: {
        access_token: "token-b",
        account_id: "acc-b",
        id_token: "id-b",
        refresh_token: "rt-b",
      },
    }),
  );

  const result = await migrateCodexAccountPool({ tokensDir: dir });
  const pool = JSON.parse(await fs.readFile(result.poolPath, "utf8"));
  assert.equal(pool.length, 2);
  assert.equal(pool[0].tokens.account_id, "acc-a");
  assert.ok(result.backupDir);

  const backupEntries = await fs.readdir(result.backupDir);
  assert.ok(backupEntries.includes("a.json"));
  assert.ok(backupEntries.includes("b.json"));
});