| 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 | nestedSessions, |
| 11 | readBookmarks, |
| 12 | rpcMethodOf, |
| 13 | serverTiming, |
| 14 | serviceDuration, |
| 15 | databaseTime, |
| 16 | sessionFor, |
| 17 | setsBookmark, |
| 18 | writeBookmarks, |
| 19 | } from "./perf.ts"; |
| 20 | |
| 21 | const BOOKMARK = "0000002c-00000004-00004f95-c7f4a9b2e8d1f0c3b6a5d4e3f2a1b0c9"; |
| 22 | |
| 23 | test("bookmarks survive the cookie", () => { |
| 24 | const value = writeBookmarks({ at: 1_800_000_000, services: { work: BOOKMARK, repos: BOOKMARK } }); |
| 25 | assert.equal(value, `at:1800000000~repos:${BOOKMARK}~work:${BOOKMARK}`); |
| 26 | assert.deepEqual(readBookmarks(`g1t_session=abc; g1t_d1=${value}; other=1`), { |
| 27 | at: 1_800_000_000, |
| 28 | services: { repos: BOOKMARK, work: BOOKMARK }, |
| 29 | }); |
| 30 | }); |
| 31 | |
| 32 | test("a malformed or foreign cookie entry is dropped", () => { |
| 33 | assert.deepEqual(readBookmarks(null), { at: null, services: {} }); |
| 34 | assert.deepEqual(readBookmarks("g1t_d1=at:soon~work:bad bookmark~actions:abc~repos:" + BOOKMARK), { |
| 35 | at: null, |
| 36 | services: { repos: BOOKMARK }, |
| 37 | }); |
| 38 | // Only a service that reads with sessions is kept. |
| 39 | assert.equal(writeBookmarks({ at: null, services: { actions: BOOKMARK } }), ""); |
| 40 | }); |
| 41 | |
| 42 | test("the cookie is HttpOnly, short-lived and Secure over HTTPS", () => { |
| 43 | const cookie = bookmarkCookie({ at: 1, services: {} }, true); |
| 44 | assert.match(cookie, /^g1t_d1=at:1; Path=\/; HttpOnly; Secure; SameSite=Lax; Max-Age=300$/); |
| 45 | assert.doesNotMatch(bookmarkCookie({ at: 1, services: {} }, false), /Secure/); |
| 46 | }); |
| 47 | |
| 48 | test("what each call asks for", () => { |
| 49 | const now = 1_800_000_000; |
| 50 | const none = { at: null, services: {} }; |
| 51 | // A service without sessions is left alone: its primary, as before. |
| 52 | assert.equal(sessionFor("actions", none, false, now), null); |
| 53 | // A form post reads the primary everywhere. |
| 54 | assert.equal(sessionFor("work", { at: null, services: { work: BOOKMARK } }, true, now), "first-primary"); |
| 55 | // A page with no recent write reads the nearest copy. |
| 56 | assert.equal(sessionFor("work", none, false, now), "first-unconstrained"); |
| 57 | // Just after a write, every service reads its primary, bookmark or not. |
| 58 | const justWrote = { at: now - 5, services: { work: BOOKMARK } }; |
| 59 | assert.equal(sessionFor("work", justWrote, false, now), "first-primary"); |
| 60 | assert.equal(sessionFor("repos", justWrote, false, now), "first-primary"); |
| 61 | // Later, the bookmark keeps the read at least as new as the write. |
| 62 | const later = { at: now - PRIMARY_WINDOW_SECONDS, services: { work: BOOKMARK } }; |
| 63 | assert.equal(sessionFor("work", later, false, now), BOOKMARK); |
| 64 | assert.equal(sessionFor("repos", later, false, now), "first-unconstrained"); |
| 65 | }); |
| 66 | |
| 67 | test("work's reads of repos go where the site's own would", () => { |
| 68 | const now = 1_800_000_000; |
| 69 | const none = { at: null, services: {} }; |
| 70 | // A page with no recent write: work asks repos' nearest copy. |
| 71 | assert.deepEqual(nestedSessions("work", "get_pull", none, false, now), [["x-d1-bookmark-repos", "first-unconstrained"]]); |
| 72 | // After a write to repos, at least as new as it. |
| 73 | const later = { at: now - PRIMARY_WINDOW_SECONDS, services: { repos: BOOKMARK } }; |
| 74 | assert.deepEqual(nestedSessions("work", "get_settings", later, false, now), [["x-d1-bookmark-repos", BOOKMARK]]); |
| 75 | // Just after any write, repos' primary. |
| 76 | const justWrote = { at: now - 5, services: {} }; |
| 77 | assert.deepEqual(nestedSessions("work", "list_pulls", justWrote, false, now), [["x-d1-bookmark-repos", "first-primary"]]); |
| 78 | // A method that may write, or a form post, says nothing: the primary. |
| 79 | assert.deepEqual(nestedSessions("work", "merge_pull", none, false, now), []); |
| 80 | assert.deepEqual(nestedSessions("work", "get_pull", none, true, now), []); |
| 81 | // Only services that read another's database on the caller's behalf. |
| 82 | assert.deepEqual(nestedSessions("repos", "get", none, false, now), []); |
| 83 | }); |
| 84 | |
| 85 | test("only known reads are taken not to write", () => { |
| 86 | for (const method of [ |
| 87 | "get_pull", |
| 88 | "list_pulls", |
| 89 | "counts", |
| 90 | "user_for_session", |
| 91 | "explore", |
| 92 | "usage", |
| 93 | "get", |
| 94 | "list", |
| 95 | "queue", |
| 96 | "pulls_for_repos", |
| 97 | "stars", |
| 98 | "about", |
| 99 | "public_links", |
| 100 | "branch_drift", |
| 101 | "tags", |
| 102 | "commit_checks", |
| 103 | "shortcuts", |
| 104 | "last_commits", |
| 105 | "languages", |
| 106 | ]) { |
| 107 | assert.equal(mayWrite(method), false, method); |
| 108 | } |
| 109 | for (const method of ["merge_pull", "verify_email", "github_finish", "sign_in", "something_new"]) { |
| 110 | assert.equal(mayWrite(method), true, method); |
| 111 | } |
| 112 | assert.equal(rpcMethodOf("https://service/rpc/get_pull"), "get_pull"); |
| 113 | assert.equal(rpcMethodOf("https://service/other"), ""); |
| 114 | }); |
| 115 | |
| 116 | test("a signed-out GET never sets the bookmark cookie, so public pages stay cacheable", () => { |
| 117 | const read = { writing: false, signedIn: false }; |
| 118 | assert.equal(setsBookmark({ ...read, wrote: true, hasSession: false }), false); |
| 119 | assert.equal(setsBookmark({ ...read, wrote: true, hasSession: true }), true); |
| 120 | assert.equal(setsBookmark({ ...read, wrote: false, hasSession: true }), false); |
| 121 | // A form post, or signing in on a GET, always does. |
| 122 | assert.equal(setsBookmark({ writing: true, signedIn: false, wrote: false, hasSession: false }), true); |
| 123 | assert.equal(setsBookmark({ writing: false, signedIn: true, wrote: false, hasSession: false }), true); |
| 124 | }); |
| 125 | |
| 126 | test("timings", () => { |
| 127 | assert.equal(serviceDuration('svc;dur=12;desc="session"'), 12); |
| 128 | assert.equal(serviceDuration("repo;dur=3, svc;dur=7.5"), 7.5); |
| 129 | assert.equal(serviceDuration(null), null); |
| 130 | assert.deepEqual(databaseTime('svc;dur=40;desc="session", db;dur=22;desc="2 round trips, 21 statements", rpc;dur=18;desc="1 calls"'), { ms: 22, trips: 2 }); |
| 131 | assert.equal(databaseTime('svc;dur=40;desc="session"'), null); |
| 132 | // Overlapping calls are counted once. |
| 133 | assert.equal( |
| 134 | coveredMs([ |
| 135 | [0, 100], |
| 136 | [50, 120], |
| 137 | [200, 210], |
| 138 | ]), |
| 139 | 130, |
| 140 | ); |
| 141 | assert.equal(coveredMs([]), 0); |
| 142 | assert.equal(metricName("routes/repo/pull"), "repo.pull"); |
| 143 | const header = serverTiming({ |
| 144 | totalMs: 180, |
| 145 | loaders: [{ id: "routes/repo/pull", ms: 150, kind: "loader" }], |
| 146 | rpcMs: 140, |
| 147 | services: { |
| 148 | work: { calls: 3, wallMs: 120, serviceMs: 90, dbMs: 40, dbTrips: 3 }, |
| 149 | repos: { calls: 1, wallMs: 30, serviceMs: 0 }, |
| 150 | }, |
| 151 | sessions: "work=unconstrained", |
| 152 | }); |
| 153 | assert.equal( |
| 154 | header, |
| 155 | 'total;dur=180;desc="web to first byte", loader.repo.pull;dur=150, rpc;dur=140;desc="4 service calls, overlap counted once", ' + |
| 156 | 'work;dur=120;desc="3 calls, 90ms inside, db 40ms in 3 round trips", repos;dur=30;desc="1 call", d1;desc="work=unconstrained"', |
| 157 | ); |
| 158 | }); |