|
| 1 | +// Seeds the Android subscription widget with a fixed fixture for the #22973 |
| 2 | +// before/after evidence. It temporarily patches the working tree so the real |
| 3 | +// snapshot -> publishSubscriptionUsage -> widget.updateSnapshot path runs on |
| 4 | +// fixture providers instead of connected environments. Nothing changes until |
| 5 | +// it runs, and `revert` restores the tracked files from HEAD. |
| 6 | +// |
| 7 | +// node scripts/seed-widget-fixture-22973.mts apply <fixture> [--anchor <iso>] [--before [ref]] |
| 8 | +// node scripts/seed-widget-fixture-22973.mts revert |
| 9 | +// node scripts/seed-widget-fixture-22973.mts list |
| 10 | +// |
| 11 | +// Fixture times are minutes from the anchor (default: now, rounded down to the |
| 12 | +// minute). Reuse one anchor for a before/after pair so both render identical |
| 13 | +// reset labels. `--before` swaps in subscriptionUsageSnapshot.ts from the |
| 14 | +// merge-base so the same hook runs over the pre-fix code. Reload the app |
| 15 | +// (Metro picks the change up) after each apply. |
| 16 | +import { execFileSync } from "node:child_process"; |
| 17 | +import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 18 | +import { dirname, join } from "node:path"; |
| 19 | +import { fileURLToPath } from "node:url"; |
| 20 | + |
| 21 | +const BEFORE_REF = "8d3b061121fe65669d5d43d011b48cd3027a4108"; |
| 22 | +const appRoot = join(dirname(fileURLToPath(import.meta.url)), ".."); |
| 23 | +const coordinator = join(appRoot, "src/widgets/SubscriptionUsageCoordinator.tsx"); |
| 24 | +const snapshotModule = join(appRoot, "src/widgets/subscriptionUsageSnapshot.ts"); |
| 25 | +const fixtureModule = join(appRoot, "src/widgets/widgetFixture22973.ts"); |
| 26 | +const tracked = [coordinator, snapshotModule]; |
| 27 | + |
| 28 | +const codex = ( |
| 29 | + checked: number, |
| 30 | + session: number, |
| 31 | + weekly: number, |
| 32 | + instance = "codex", |
| 33 | + |
| 34 | +) => |
| 35 | + `prov("codex", ${JSON.stringify(instance)}, ${JSON.stringify(email)}, ${checked}, [W("primary", "session", "5 hours", 0, ${session}, 300), W("secondary", "weekly", "Weekly", 30, ${weekly}, 10080)])`; |
| 36 | +const week = 7 * 1440; |
| 37 | +const fixtures: Record<string, string[]> = { |
| 38 | + F1: [codex(0, 45, week)], |
| 39 | + F2: [codex(0, -15, week)], |
| 40 | + F3: [codex(0, -15, -1)], |
| 41 | + F4: [codex(0, 0, week)], |
| 42 | + F5: [codex(0, 5, week)], |
| 43 | + F6: [codex(-16, 45, week)], |
| 44 | + F7a: [ |
| 45 | + codex(-5, 45, week, "codex-0", "[email protected]"), |
| 46 | + codex(0, -3, week, "codex-1", "[email protected]"), |
| 47 | + ], |
| 48 | + F7b: [ |
| 49 | + codex(-5, -3, week, "codex-0", "[email protected]"), |
| 50 | + codex(0, 45, week, "codex-1", "[email protected]"), |
| 51 | + ], |
| 52 | + F8: [ |
| 53 | + `prov("claudeAgent", "claude", "[email protected]", 0, [W("five_hour", "session", "Session", 10, -20, 300), W("seven_day", "weekly", "Weekly", 50, ${6 * 1440}, 10080), W("seven_day_claude_opus_4_7_extended_thinking", "weekly", "Weekly · Claude Opus 4.7 Extended Thinking Preview", 95, ${6 * 1440})])`, |
| 54 | + ], |
| 55 | + F9: [ |
| 56 | + codex(0, -15, week), |
| 57 | + `prov("claudeAgent", "claude", "[email protected]", 0, [W("five_hour", "session", "Session", 10, 120, 300)])`, |
| 58 | + ], |
| 59 | +}; |
| 60 | + |
| 61 | +const git = (...args: string[]) => execFileSync("git", args, { cwd: appRoot, encoding: "utf8" }); |
| 62 | + |
| 63 | +function revert() { |
| 64 | + git("checkout", "HEAD", "--", ...tracked); |
| 65 | + rmSync(fixtureModule, { force: true }); |
| 66 | +} |
| 67 | + |
| 68 | +function apply(name: string, anchor: number, beforeRef: string | undefined) { |
| 69 | + const providers = fixtures[name]; |
| 70 | + if (!providers) |
| 71 | + throw new Error(`Unknown fixture ${name}; known: ${Object.keys(fixtures).join(", ")}`); |
| 72 | + if (!existsSync(fixtureModule) && git("status", "--porcelain", "--", ...tracked).trim()) |
| 73 | + throw new Error("Widget sources have uncommitted changes; commit or revert them first."); |
| 74 | + revert(); |
| 75 | + if (beforeRef) |
| 76 | + writeFileSync( |
| 77 | + snapshotModule, |
| 78 | + git("show", `${beforeRef}:apps/mobile/src/widgets/subscriptionUsageSnapshot.ts`), |
| 79 | + ); |
| 80 | + writeFileSync( |
| 81 | + fixtureModule, |
| 82 | + `// Generated by scripts/seed-widget-fixture-22973.mts; removed by its revert command. |
| 83 | +const ANCHOR = ${anchor}; // ${new Date(anchor).toISOString()} |
| 84 | +const iso = (minutes: number) => new Date(ANCHOR + minutes * 60_000).toISOString(); |
| 85 | +const W = (id: string, kind: string, label: string, usedPercent: number, reset: number, windowDurationMins?: number) => ({ |
| 86 | + id, kind, label, usedPercent, resetsAt: iso(reset), ...(windowDurationMins ? { windowDurationMins } : {}), |
| 87 | +}); |
| 88 | +const prov = (driver: string, instanceId: string, email: string, checked: number, windows: unknown[]) => ({ |
| 89 | + instanceId, driver, enabled: true, installed: true, version: null, status: "ready", |
| 90 | + auth: { status: "authenticated", email }, checkedAt: iso(checked), models: [], slashCommands: [], skills: [], |
| 91 | + usageLimits: { checkedAt: iso(checked), windows }, |
| 92 | +}); |
| 93 | +export function widgetFixture22973<T>(): T { |
| 94 | + return new Map([["env", { entry: { target: { label: "Remote" } }, serverConfig: { providers: [ |
| 95 | + ${providers.join(",\n ")}, |
| 96 | + ] } }]]) as T; |
| 97 | +} |
| 98 | +`, |
| 99 | + ); |
| 100 | + const source = readFileSync(coordinator, "utf8"); |
| 101 | + const read = "get(environmentPresentations.presentationsWithProvidersAtom),"; |
| 102 | + if (!source.includes(read)) |
| 103 | + throw new Error("SubscriptionUsageCoordinator.tsx no longer matches the hook"); |
| 104 | + writeFileSync( |
| 105 | + coordinator, |
| 106 | + source |
| 107 | + .replace(read, `widgetFixture22973<Parameters<typeof buildSubscriptionUsageSnapshot>[0]>(),`) |
| 108 | + .replace( |
| 109 | + `import { createWidgetTheme } from "./widgetTheme";`, |
| 110 | + `import { createWidgetTheme } from "./widgetTheme";\nimport { widgetFixture22973 } from "./widgetFixture22973";`, |
| 111 | + ), |
| 112 | + ); |
| 113 | + console.log( |
| 114 | + `Applied ${name} (${beforeRef ? `before: ${beforeRef.slice(0, 10)}` : "after: HEAD"}) anchored at ${new Date(anchor).toISOString()}`, |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +const [command, name, ...rest] = process.argv.slice(2); |
| 119 | +const flag = (key: string) => { |
| 120 | + const index = rest.indexOf(key); |
| 121 | + if (index === -1) return undefined; |
| 122 | + const value = rest[index + 1]; |
| 123 | + return value && !value.startsWith("--") ? value : ""; |
| 124 | +}; |
| 125 | +if (command === "apply" && name) { |
| 126 | + const anchorFlag = flag("--anchor"); |
| 127 | + const anchor = anchorFlag ? Date.parse(anchorFlag) : Math.floor(Date.now() / 60_000) * 60_000; |
| 128 | + if (!Number.isFinite(anchor)) throw new Error(`Invalid --anchor ${anchorFlag}`); |
| 129 | + const before = flag("--before"); |
| 130 | + apply(name, anchor, before === undefined ? undefined : before || BEFORE_REF); |
| 131 | +} else if (command === "revert") { |
| 132 | + revert(); |
| 133 | + console.log("Reverted widget fixture hook"); |
| 134 | +} else if (command === "list") { |
| 135 | + console.log(Object.keys(fixtures).join("\n")); |
| 136 | +} else { |
| 137 | + console.error( |
| 138 | + "Usage: seed-widget-fixture-22973.mts apply <fixture> [--anchor <iso>] [--before [ref]] | revert | list", |
| 139 | + ); |
| 140 | + process.exit(1); |
| 141 | +} |
0 commit comments