aboutsummaryrefslogtreecommitdiffstats
path: root/test-ark-sync.mjs
blob: ce318e0309bd1cf1f3db49334ad0043f1a3188f2 (plain) (blame)
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
import { chromium } from "playwright";

const BASE = "http://172.18.0.9:3000";
const API = "http://172.18.0.12:3119";
const API_KEY = "test-playwright-key";
const ARK_KEY = "1fed5c62c46ea3f72757e0baf1b83573ffd125eaba69a61c0f847c0d26d7b924";

(async () => {
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext({ ignoreHTTPSErrors: true });
  const page = await context.newPage();

  page.on("console", (msg) => console.log(`[browser:${msg.type()}]`, msg.text()));
  page.on("pageerror", (err) => console.log("[page error]", err.message));

  // Monitor ALL arkade.computer requests
  page.on("request", (req) => {
    const u = req.url();
    if (u.includes("arkade.computer")) {
      console.log(`[req] ${req.method()} ${u} ${req.postData()?.substring(0, 300) || ""}`);
    }
  });
  page.on("response", (resp) => {
    const u = resp.url();
    if (u.includes("arkade.computer") && !u.includes("subscription/")) {
      resp
        .text()
        .then((t) => console.log(`[resp] ${resp.status()} ${u} -> ${t.substring(0, 500)}`));
    }
  });

  // 1. Login
  console.log("--- Logging in ---");
  const loginResp = await page.request.post(`${API}/login`, {
    data: { username: "bob", password: "pw" },
    headers: { "content-type": "application/json", "x-api-key": API_KEY },
  });
  console.log("Login:", loginResp.status());
  const allHeaders = await loginResp.headersArray();
  for (const h of allHeaders) {
    if (h.name.toLowerCase() === "set-cookie") {
      const m = h.value.match(/token=([^;]+)/);
      if (m)
        await context.addCookies([{ name: "token", value: m[1], domain: "172.18.0.9", path: "/" }]);
    }
  }

  // 2. Go to bob's page, set arkkey
  await page.goto(`${BASE}/bob`, { waitUntil: "domcontentloaded" });
  await page.evaluate((key) => localStorage.setItem("arkkey", JSON.stringify(key)), ARK_KEY);

  // 3. Reload — don't wait for networkidle since SSE subscription stays open
  console.log("--- Reloading with arkkey ---");
  await page.goto(`${BASE}/bob`, { waitUntil: "domcontentloaded" });
  console.log("--- Waiting 15s for SDK ---");
  await page.waitForTimeout(15000);

  // 4. Check SDK state by evaluating in browser context
  console.log("\n--- Checking SDK balance/vtxos/history ---");
  // The app's getWallet() should have created a wallet instance.
  // We can't import the module directly, but we can check what the accounts endpoint returns.
  const accountsData = await page.request.get(`${API}/accounts`, {
    headers: {
      "x-api-key": API_KEY,
      cookie: (await context.cookies()).map((c) => `${c.name}=${c.value}`).join("; "),
    },
  });
  console.log("Accounts:", (await accountsData.text()).substring(0, 500));

  // 5. Check server logs
  console.log("\n--- Done. Check docker logs app for arkSync entries ---");
  await browser.close();
})();