Repository navigation
fix(mobile): client storage sizes no longer read "1024 KB" #41599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tiliakoos
wants to merge
3
commits into
pingdotgg:main
Choose a base branch
from
tiliakoos:fix/mobile-storage-format-bytes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−4
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { formatBytes } from "./formatBytes"; | ||
|
|
||
| describe("formatBytes", () => { | ||
| it.each([ | ||
| [1_023, "1023 B"], | ||
| [1_047_552, "1023 KB"], | ||
| [1_048_575, "1.0 MB"], | ||
| [1_073_741_823, "1.0 GB"], | ||
| [24 * 1024 ** 3, "24.0 GB"], | ||
| ])("formats %d bytes as %s", (bytes, label) => { | ||
| expect(formatBytes(bytes)).toBe(label); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| const UNITS = ["KB", "MB", "GB"] as const; | ||
|
|
||
| export function formatBytes(bytes: number): string { | ||
| if (bytes < 1024) return `${bytes} B`; | ||
| if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(bytes < 10 * 1024 ? 1 : 0)} KB`; | ||
| if (bytes < 1024 ** 3) | ||
| return `${(bytes / (1024 * 1024)).toFixed(bytes < 10 * 1024 * 1024 ? 1 : 0)} MB`; | ||
| return `${(bytes / 1024 ** 3).toFixed(1)} GB`; | ||
| let value = bytes / 1024; | ||
| let unitIndex = 0; | ||
| while (true) { | ||
| const isLast = unitIndex === UNITS.length - 1; | ||
| const label = value.toFixed(isLast || value < 10 ? 1 : 0); | ||
| // Pick the unit after rounding, so 1023.9 KB reads "1.0 MB", not "1024 KB". | ||
| if (isLast || Number(label) < 1024) return `${label} ${UNITS[unitIndex]}`; | ||
| value /= 1024; | ||
| unitIndex += 1; | ||
| } | ||
| } | ||
111 changes: 111 additions & 0 deletions
111
scripts/scenarios/client-storage-does-not-read-1024-kb.e2e.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import { expect } from "e2e"; | ||
|
|
||
| import { MOBILE, MOBILE_T3_HOME } from "../e2e.config.ts"; | ||
| import { environmentId, makeState, runPromise } from "./t3.ts"; | ||
| import { IOS, test } from "./t3-mobile.ts"; | ||
|
|
||
| const APP_ID = "com.t3tools.t3code.dev"; | ||
| const SCHEME = "t3code-dev"; | ||
| const TARGET_BYTES = 1_048_320; | ||
| const BYTES_PER_CHAR = 2; | ||
| const TURN_OVERHEAD = 10_590; | ||
| const DRIFT_BYTES = 32; | ||
|
|
||
| function parseClearSize(label: string): { readonly lo: number; readonly hi: number } { | ||
| const match = /^Clear (\d+(?:\.\d+)?) (B|KB|MB)$/.exec(label.trim()); | ||
| if (match === null) throw new Error(`Unexpected Client Storage button label: ${label}`); | ||
| const value = Number(match[1]); | ||
| const unit = match[2]; | ||
| if (unit === "B") return { lo: value, hi: value + 1 }; | ||
| const scale = unit === "MB" ? 1024 * 1024 : 1024; | ||
| const halfStep = match[1]?.includes(".") ? 0.05 : 0.5; | ||
| return { lo: (value - halfStep) * scale, hi: (value + halfStep) * scale }; | ||
| } | ||
|
|
||
| test( | ||
| 'Client storage sizes just under 1 MB read "1.0 MB" instead of "1024 KB"', | ||
| IOS, | ||
| async ({ t3, app, screen, device }) => { | ||
| const project = await t3.project("demo"); | ||
| const pad = await t3.thread({ | ||
| project, | ||
| title: "Calibration", | ||
| messages: ["seed"], | ||
| }); | ||
| const { cli } = makeState(MOBILE_T3_HOME, { T3CODE_PORT: String(MOBILE.serverPort) }); | ||
|
|
||
| // Start from an empty cache so repeated local simulator runs match a fresh device. | ||
| await app.clearState().catch(() => {}); | ||
| const [, bulk] = await Promise.all([ | ||
| t3.open(), | ||
| t3.thread({ | ||
| project, | ||
| title: "Bulk cache", | ||
| messages: Array.from({ length: 4 }, (_, index) => `bulk-${index} ${"a".repeat(104_000)}`), | ||
| }), | ||
| ]); | ||
| await screen.getByText("Bulk cache", { exact: false }).first().waitFor({ timeout: 120_000 }); | ||
|
|
||
| const environment = await runPromise( | ||
| environmentId(`http://${MOBILE.host}:${MOBILE.serverPort}`), | ||
| ); | ||
| const openLink = async (url: string) => { | ||
| await device.openLink(url, { app: APP_ID }); | ||
| }; | ||
| const openThreadAndFlush = async (threadId: string, marker: string) => { | ||
| await openLink(`${SCHEME}://threads/${environment}/${threadId}`); | ||
| await screen.getByText(marker, { exact: false }).first().waitFor({ timeout: 60_000 }); | ||
| await screen.getByTestId("BackButton").tap(); | ||
| await screen.getByRole("tablist", "Tab Bar").waitFor({ timeout: 30_000 }); | ||
| }; | ||
| const readClientStorage = async () => { | ||
| await openLink(`${SCHEME}://settings/client-storage`); | ||
| const clearButton = screen.getByRole("button", /^Clear \d/); | ||
| await clearButton.waitFor({ timeout: 30_000 }); | ||
| const label = (await clearButton.textContent()) ?? ""; | ||
| await screen.getByTestId("BackButton").tap(); | ||
| await screen.getByRole("tablist", "Tab Bar").waitFor({ timeout: 30_000 }); | ||
| return parseClearSize(label); | ||
| }; | ||
|
|
||
| await openThreadAndFlush(bulk.threadId, "first fixture turn complete"); | ||
| await openThreadAndFlush(pad.threadId, "first fixture turn complete"); | ||
| let { lo, hi } = await readClientStorage(); | ||
|
|
||
| // Narrow the SQLite cache total to ±128 B via half-KB rounding steps, then land inside | ||
| // [1,048,064, 1,048,575] where base renders "1024 KB" and head renders "1.0 MB". | ||
| for (let step = 1; step <= 6; step += 1) { | ||
| const width = hi - lo; | ||
| const mid = (lo + hi) / 2; | ||
| const done = width <= 320; | ||
| const minNext = mid + TURN_OVERHEAD + 100; | ||
| const nextHalfKb = Math.ceil((minNext - 512) / 1024) * 1024 + 512; | ||
| const target = done ? TARGET_BYTES : Math.max(nextHalfKb, 980.5 * 1024); | ||
| const padChars = Math.max(16, Math.round((target - mid - TURN_OVERHEAD) / BYTES_PER_CHAR)); | ||
| const expectedGrowth = TURN_OVERHEAD + padChars * BYTES_PER_CHAR; | ||
| const tag = `pad-${step}`; | ||
| const body = `${tag} ${"a".repeat(Math.max(1, padChars - tag.length - 1))}`; | ||
|
|
||
| await runPromise(cli(["thread", "send", pad.threadId, body, "--wait", "--json"], [0, 2])); | ||
| await openThreadAndFlush(pad.threadId, tag); | ||
| if (done) break; | ||
|
|
||
| const observed = await readClientStorage(); | ||
| const predLo = lo + expectedGrowth - DRIFT_BYTES; | ||
| const predHi = hi + expectedGrowth + DRIFT_BYTES; | ||
| if (Math.max(predLo, observed.lo) < Math.min(predHi, observed.hi)) { | ||
| lo = Math.max(predLo, observed.lo); | ||
| hi = Math.min(predHi, observed.hi); | ||
| } else { | ||
| lo = observed.lo; | ||
| hi = observed.hi; | ||
| } | ||
| } | ||
|
|
||
| await openLink(`${SCHEME}://settings/client-storage`); | ||
| await screen.getByRole("button", /^Clear \d/).waitFor({ timeout: 30_000 }); | ||
| await app.screenshot("client-storage"); | ||
| await expect(screen.getByRole("button", "Clear 1.0 MB")).toBeVisible(); | ||
| await expect(screen.getByRole("button", /1024 KB/)).not.toBeVisible(); | ||
| }, | ||
| ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the decimal when KB or MB rounds to 10.
For
10 * 1024 - 1bytes,value < 10selects one decimal and returns"10.0 KB". The equivalent MB boundary returns"10.0 MB". This leaves the stated rounding defect unresolved.If KB or MB rounds to 10, format it without a decimal. Preserve one decimal for GB. Add these boundary inputs to the existing test table.