forked from steipete/CodexBar
-
Notifications
You must be signed in to change notification settings - Fork 137
[0.60.5] Port usage spend sharing semantics #549
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
Finesssee
wants to merge
2
commits into
main
Choose a base branch
from
codex/port-0.60.5-sharing-impl
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.
Open
Changes from all commits
Commits
Show all changes
2 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
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,39 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| formatUsageSpendReportingDay, | ||
| usageSpendShareFooter, | ||
| usageSpendSubscriptionCaption, | ||
| } from "./usageSpendSharing"; | ||
|
|
||
| describe("usage spend sharing", () => { | ||
| it.each([ | ||
| [0, "0 subscriptions"], | ||
| [1, "1 subscription"], | ||
| [2, "2 subscriptions"], | ||
| [12, "12 subscriptions"], | ||
| ])("uses the correct subscription caption for %i", (count, expected) => { | ||
| expect(usageSpendSubscriptionCaption(count)).toBe(expected); | ||
| }); | ||
|
|
||
| it("preserves the reporting day across dashboard timezones", () => { | ||
| expect(formatUsageSpendReportingDay("2026-09-19", "Pacific/Kiritimati")).toBe("Sep 19, 2026"); | ||
| expect(formatUsageSpendReportingDay("2026-09-19", "America/Los_Angeles")).toBe("Sep 19, 2026"); | ||
| expect(formatUsageSpendReportingDay("2026-10-01", "Pacific/Norfolk")).toBe("Oct 1, 2026"); | ||
| }); | ||
|
|
||
| it("falls back for an invalid dashboard timezone or date", () => { | ||
| expect(formatUsageSpendReportingDay("2026-10-01", "Not/AZone")).toBe("2026-10-01"); | ||
| expect(formatUsageSpendReportingDay("2026-02-31", "UTC")).toBe("2026-02-31"); | ||
| }); | ||
|
|
||
| it("builds the report footer from the included day and row count", () => { | ||
| expect( | ||
| usageSpendShareFooter({ | ||
| rows: [{}], | ||
| reportingDay: "2026-09-19", | ||
| dashboardTimezone: "UTC", | ||
| }), | ||
| ).toBe("Data through Sep 19, 2026 · 1 subscription"); | ||
| }); | ||
| }); |
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,92 @@ | ||
| interface CivilDate { | ||
| year: number; | ||
| month: number; | ||
| day: number; | ||
| } | ||
|
|
||
| export interface UsageSpendShareSummary { | ||
| rows: readonly unknown[]; | ||
| reportingDay: string; | ||
| dashboardTimezone: string; | ||
| } | ||
|
|
||
| function parseCivilDate(value: string): CivilDate | null { | ||
| const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value); | ||
| if (!match) return null; | ||
| const [, yearText, monthText, dayText] = match; | ||
| const year = Number(yearText); | ||
| const month = Number(monthText); | ||
| const day = Number(dayText); | ||
| if (year < 1 || month < 1 || month > 12 || day < 1 || day > 31) return null; | ||
|
|
||
| const instant = new Date(0); | ||
| instant.setUTCFullYear(year, month - 1, day); | ||
| instant.setUTCHours(12, 0, 0, 0); | ||
| if ( | ||
| instant.getUTCFullYear() !== year || | ||
| instant.getUTCMonth() !== month - 1 || | ||
| instant.getUTCDate() !== day | ||
| ) { | ||
| return null; | ||
| } | ||
| return { year, month, day }; | ||
| } | ||
|
|
||
| function civilDateInstant(date: CivilDate): Date { | ||
| const instant = new Date(0); | ||
| instant.setUTCFullYear(date.year, date.month - 1, date.day); | ||
| instant.setUTCHours(12, 0, 0, 0); | ||
| return instant; | ||
| } | ||
|
|
||
| function partsInTimeZone(instant: Date, timeZone: string): CivilDate { | ||
| const parts = new Intl.DateTimeFormat("en-US", { | ||
| timeZone, | ||
| year: "numeric", | ||
| month: "2-digit", | ||
| day: "2-digit", | ||
| }).formatToParts(instant); | ||
| const valueFor = (type: string) => parts.find((part) => part.type === type)?.value; | ||
| const year = Number(valueFor("year")); | ||
| const month = Number(valueFor("month")); | ||
| const day = Number(valueFor("day")); | ||
| if (![year, month, day].every(Number.isInteger)) { | ||
| throw new RangeError("Timezone did not provide a complete date"); | ||
| } | ||
| return { year, month, day }; | ||
| } | ||
|
|
||
| /** Formats a civil reporting day without allowing the timezone offset to roll it into another day. */ | ||
| export function formatUsageSpendReportingDay( | ||
| reportingDay: string, | ||
| dashboardTimezone: string, | ||
| locale = "en-US", | ||
| ): string { | ||
| const civilDate = parseCivilDate(reportingDay); | ||
| if (!civilDate) return reportingDay; | ||
|
|
||
| try { | ||
| const target = civilDateInstant(civilDate); | ||
| // Validate the configured zone, but keep the reporting day as the civil | ||
| // date supplied by the dashboard. Applying the zone offset to the instant | ||
| // can move October 1 (and other boundary dates) into a different calendar | ||
| // day in zones with a non-hour offset or a DST transition. | ||
| partsInTimeZone(target, dashboardTimezone); | ||
| return new Intl.DateTimeFormat(locale, { | ||
| timeZone: "UTC", | ||
| year: "numeric", | ||
| month: "short", | ||
| day: "numeric", | ||
| }).format(target); | ||
| } catch { | ||
| return reportingDay; | ||
| } | ||
| } | ||
|
|
||
| export function usageSpendSubscriptionCaption(count: number): string { | ||
| return count === 1 ? "1 subscription" : `${count} subscriptions`; | ||
| } | ||
|
|
||
| export function usageSpendShareFooter(summary: UsageSpendShareSummary): string { | ||
| return `Data through ${formatUsageSpendReportingDay(summary.reportingDay, summary.dashboardTimezone)} · ${usageSpendSubscriptionCaption(summary.rows.length)}`; | ||
| } | ||
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.