| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { |
| 5 | PRIMARY_WINDOW_SECONDS, |
| 6 | bookmarkCookie, |
| 7 | coveredMs, |
| 8 | mayWrite, |
| 9 | metricName, |
| 10 | readBookmarks, |
| 11 | rpcMethodOf, |
| 12 | serverTiming, |
| 13 | serviceDuration, |
| 14 | sessionFor, |
| 15 | writeBookmarks, |
| 16 | } from "./perf.ts"; |
| 17 | |
| 18 | const BOOKMARK = "0000002c-00000004-00004f95-c7f4a9b2e8d1f0c3b6a5d4e3f2a1b0c9"; |
| 19 | |
| 20 | test("bookmarks survive the cookie", () => { |
| 21 | const value = writeBookmarks({ at: 1_800_000_000, services: { work: BOOKMARK, repos: BOOKMARK } }); |
| 22 | assert.equal(value, `at:1800000000~repos:${BOOKMARK}~work:${BOOKMARK}`); |
| 23 | assert.deepEqual(readBookmarks(`g1t_session=abc; g1t_d1=${value}; other=1`), { |
| 24 | at: 1_800_000_000, |
| 25 | services: { repos: BOOKMARK, work: BOOKMARK }, |
| 26 | }); |
| 27 | }); |
| 28 | |
| 29 | test("a malformed or foreign cookie entry is dropped", () => { |
| 30 | assert.deepEqual(readBookmarks(null), { at: null, services: {} }); |
| 31 | assert.deepEqual(readBookmarks("g1t_d1=at:soon~work:bad bookmark~actions:abc~repos:" + BOOKMARK), { |
| 32 | at: null, |
| 33 | services: { repos: BOOKMARK }, |
| 34 | }); |
| 35 | // Only a service that reads with sessions is kept. |
| 36 | assert.equal(writeBookmarks({ at: null, services: { actions: BOOKMARK } }), ""); |
| 37 | }); |
| 38 | |
| 39 | test("the cookie is HttpOnly, short-lived and Secure over HTTPS", () => { |
| 40 | const cookie = bookmarkCookie({ at: 1, services: {} }, true); |
| 41 | assert.match(cookie, /^g1t_d1=at:1; Path=\/; HttpOnly; Secure; SameSite=Lax; Max-Age=300$/); |
| 42 | assert.doesNotMatch(bookmarkCookie({ at: 1, services: {} }, false), /Secure/); |
| 43 | }); |
| 44 | |
| 45 | test("what each call asks for", () => { |
| 46 | const now = 1_800_000_000; |
| 47 | const none = { at: null, services: {} }; |
| 48 | // A service without sessions is left alone: its primary, as before. |
| 49 | assert.equal(sessionFor("actions", none, false, now), null); |
| 50 | // A form post reads the primary everywhere. |
| 51 | assert.equal(sessionFor("work", { at: null, services: { work: BOOKMARK } }, true, now), "first-primary"); |
| 52 | // A page with no recent write reads the nearest copy. |
| 53 | assert.equal(sessionFor("work", none, false, now), "first-unconstrained"); |
| 54 | // Just after a write, every service reads its primary, bookmark or not. |
| 55 | const justWrote = { at: now - 5, services: { work: BOOKMARK } }; |
| 56 | assert.equal(sessionFor("work", justWrote, false, now), "first-primary"); |
| 57 | assert.equal(sessionFor("repos", justWrote, false, now), "first-primary"); |
| 58 | // Later, the bookmark keeps the read at least as new as the write. |
| 59 | const later = { at: now - PRIMARY_WINDOW_SECONDS, services: { work: BOOKMARK } }; |
| 60 | assert.equal(sessionFor("work", later, false, now), BOOKMARK); |
| 61 | assert.equal(sessionFor("repos", later, false, now), "first-unconstrained"); |
| 62 | }); |
| 63 | |
| 64 | test("only known reads are taken not to write", () => { |
| 65 | for (const method of ["get_pull", "list_pulls", "counts", "user_for_session", "explore", "usage"]) { |
| 66 | assert.equal(mayWrite(method), false, method); |
| 67 | } |
| 68 | for (const method of ["merge_pull", "verify_email", "github_finish", "sign_in", "something_new"]) { |
| 69 | assert.equal(mayWrite(method), true, method); |
| 70 | } |
| 71 | assert.equal(rpcMethodOf("https://service/rpc/get_pull"), "get_pull"); |
| 72 | assert.equal(rpcMethodOf("https://service/other"), ""); |
| 73 | }); |
| 74 | |
| 75 | test("timings", () => { |
| 76 | assert.equal(serviceDuration('svc;dur=12;desc="session"'), 12); |
| 77 | assert.equal(serviceDuration("repo;dur=3, svc;dur=7.5"), 7.5); |
| 78 | assert.equal(serviceDuration(null), null); |
| 79 | // Overlapping calls are counted once. |
| 80 | assert.equal(coveredMs([[0, 100], [50, 120], [200, 210]]), 130); |
| 81 | assert.equal(coveredMs([]), 0); |
| 82 | assert.equal(metricName("routes/repo/pull"), "repo.pull"); |
| 83 | const header = serverTiming({ |
| 84 | totalMs: 180, |
| 85 | loaders: [{ id: "routes/repo/pull", ms: 150, kind: "loader" }], |
| 86 | rpcMs: 140, |
| 87 | services: { |
| 88 | work: { calls: 3, wallMs: 120, serviceMs: 90 }, |
| 89 | repos: { calls: 1, wallMs: 30, serviceMs: 0 }, |
| 90 | }, |
| 91 | sessions: "work=unconstrained", |
| 92 | }); |
| 93 | assert.equal( |
| 94 | header, |
| 95 | 'total;dur=180;desc="web to first byte", loader.repo.pull;dur=150, rpc;dur=140;desc="4 service calls, overlap counted once", ' + |
| 96 | 'work;dur=120;desc="3 calls, 90ms inside", repos;dur=30;desc="1 call", d1;desc="work=unconstrained"', |
| 97 | ); |
| 98 | }); |