Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions apps/mobile/src/features/settings/environment-maintenance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,45 +159,53 @@ describe("environment maintenance access", () => {

describe("environment release checks", () => {
const signal = new AbortController().signal;

it("keeps stable hosts on stable releases and ignores drafts", async () => {
vi.stubGlobal(
"fetch",
vi
.fn()
.mockImplementation(async () =>
Response.json([
{ tag_name: "v2.0.0-nightly.20260923.1" },
{ tag_name: "v1.2.0", draft: true },
{ tag_name: "v1.1.0" },
]),
),
const feed = (...tags: ReadonlyArray<string>) =>
new Response(
`<feed><id>tag:github.com,2008:https://github.com/pingdotgg/t3code/releases</id>${tags
.map((tag) => `<entry><id>tag:github.com,2008:Repository/1/${tag}</id></entry>`)
.join("")}</feed>`,
);

it("offers the latest stable release to stable hosts", async () => {
const fetchMock = vi.fn().mockImplementation(async () => Response.json({ tag_name: "v1.1.0" }));
vi.stubGlobal("fetch", fetchMock);
expect(await findEnvironmentUpdate("1.0.0", signal)).toBe("1.1.0");
expect(await findEnvironmentUpdate("1.1.0", signal)).toBeNull();
expect(await findEnvironmentUpdate("1.3.0", signal)).toBeNull();
expect(fetchMock.mock.calls[0]?.[0]).toBe(
"https://github.com/pingdotgg/t3code/releases/latest",
);
});

it("walks release pages to find the host's channel", async () => {
it("finds the newest release on the host's prerelease channel in the feed", async () => {
const fetchMock = vi
.fn()
.mockResolvedValueOnce(
Response.json(Array.from({ length: 100 }, () => ({ tag_name: "v2.0.0" }))),
)
.mockResolvedValueOnce(Response.json([{ tag_name: "v1.0.0-preview.20260923.2" }]));
.mockImplementation(async () =>
feed(
"v1.0.0-preview.20260924.3",
"v1.0.0-nightly.20260924.2",
"v1.0.0",
"v1.0.0-nightly.20260923.1",
),
);
vi.stubGlobal("fetch", fetchMock);
expect(await findEnvironmentUpdate("1.0.0-nightly.20260923.1", signal)).toBe(
"1.0.0-nightly.20260924.2",
);
expect(await findEnvironmentUpdate("1.0.0-nightly.20260924.2", signal)).toBeNull();
expect(await findEnvironmentUpdate("1.0.0-preview.20260923.1", signal)).toBe(
"1.0.0-preview.20260923.2",
"1.0.0-preview.20260924.3",
);
expect(fetchMock.mock.calls[1]?.[0]).toContain("page=2");
expect(fetchMock.mock.calls[1]?.[1]).toEqual({ signal });
expect(fetchMock.mock.calls[0]?.[0]).toBe("https://github.com/pingdotgg/t3code/releases.atom");
});

it("reports failed checks instead of claiming the server is current", async () => {
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(null, { status: 403 })));
await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow("403");
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json([])));
await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow("No stable release");
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(null, { status: 429 })));
await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow("429");
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(feed("v1.0.0")));
await expect(findEnvironmentUpdate("1.0.0-nightly.20260923.1", signal)).rejects.toThrow(
"No nightly release",
);
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json({ error: "invalid" })));
await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow();
});
Expand Down
52 changes: 34 additions & 18 deletions apps/mobile/src/features/settings/environment-maintenance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
type ServerProvider,
} from "@t3tools/contracts";
import {
CLI_RELEASES_PAGE_URL,
type CliReleaseChannel,
cliReleaseChannelOf,
cliReleaseIndexPageUrl,
newestCliReleaseVersion,
} from "@t3tools/shared/cliRelease";
import { compareSemverVersions } from "@t3tools/shared/semver";
Expand Down Expand Up @@ -45,25 +46,40 @@ export function canUpdateEnvironmentProvider(provider: ServerProvider) {
);
}

const Releases = Schema.Array(
Schema.Struct({
tag_name: Schema.String,
draft: Schema.optionalKey(Schema.Boolean),
}),
);
const decodeReleases = Schema.decodeUnknownSync(Releases);
const decodeLatestRelease = Schema.decodeUnknownSync(Schema.Struct({ tag_name: Schema.String }));
const FEED_TAG = /<id>tag:github\.com,2008:Repository\/\d+\/([^<]+)<\/id>/g;

// Reads github.com rather than api.github.com, whose 60 anonymous requests an
// hour per IP are shared with every other device on the phone's network.
async function fetchReleases(path: "/latest" | ".atom", accept: string, signal: AbortSignal) {
const response = await fetch(`${CLI_RELEASES_PAGE_URL}${path}`, {
signal,
headers: { Accept: accept },
});
if (!response.ok) throw new Error(`Could not check releases (${response.status}). Try again.`);
return response;
}

/**
* Stable hosts read the latest release. Nightly and preview are never marked
* latest, so they read the feed of the ten newest releases, which nightly's
* several-a-day cadence keeps current.
*/
async function channelReleases(channel: CliReleaseChannel, signal: AbortSignal) {
if (channel === "stable") {
const response = await fetchReleases("/latest", "application/json", signal);
return [decodeLatestRelease(await response.json())];
}
const response = await fetchReleases(".atom", "application/atom+xml", signal);
return Array.from((await response.text()).matchAll(FEED_TAG), ([, tag_name = ""]) => ({
tag_name,
}));
}

/** Preserve the host's release channel and never offer a downgrade. */
export async function findEnvironmentUpdate(currentVersion: string, signal: AbortSignal) {
const channel = cliReleaseChannelOf(currentVersion);
for (let page = 1; ; page++) {
const response = await fetch(cliReleaseIndexPageUrl(page), { signal });
if (!response.ok) throw new Error(`Could not check releases (${response.status}). Try again.`);
const releases = decodeReleases(await response.json());
const version = newestCliReleaseVersion(releases, channel);
if (version !== undefined) {
return compareSemverVersions(version, currentVersion) > 0 ? version : null;
}
if (releases.length < 100) throw new Error(`No ${channel} release was found.`);
}
const version = newestCliReleaseVersion(await channelReleases(channel, signal), channel);
if (version === undefined) throw new Error(`No ${channel} release was found.`);
return compareSemverVersions(version, currentVersion) > 0 ? version : null;
}
8 changes: 8 additions & 0 deletions packages/shared/src/cliRelease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ export function cliReleaseIndexPageUrl(page: number): string {
return `https://api.github.com/repos/${CLI_RELEASE_REPOSITORY}/releases?per_page=100&page=${page}`;
}

/**
* github.com release pages, which the desktop updater also reads. Unlike the
* API they have no anonymous hourly quota shared by everything behind one IP.
* `/latest` answers JSON for the newest stable release; `.atom` lists the ten
* newest releases on every channel.
*/
export const CLI_RELEASES_PAGE_URL = `https://github.com/${CLI_RELEASE_REPOSITORY}/releases`;

/**
* Picks the newest version on a channel from the release index. Tags are
* `v<version>`; the channel is decided by the same rule the runtime uses, so
Expand Down