Skip to content
Open
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
54 changes: 54 additions & 0 deletions src/__tests__/components/context-window-demo.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import fs from "fs";
import path from "path";

// Render the demo with real book translations for the chosen locale.
let currentLocale = "en";

function bookMessages(locale: string): Record<string, unknown> {
const file = path.join(process.cwd(), "messages", `${locale}.json`);
return JSON.parse(fs.readFileSync(file, "utf8")).book.interactive;
}

vi.mock("next-intl", () => ({
useLocale: () => currentLocale,
useTranslations: () => (key: string) => {
const value = bookMessages(currentLocale)[key];
return typeof value === "string" ? value : key;
},
}));

import { ContextWindowDemo } from "@/components/book/elements/demos";

// Testing Library collapses whitespace in the page text (including the
// no-break spaces some locales use as digit separators), so do the same here.
const text = (value: string) => value.replace(/\s+/g, " ");

describe("ContextWindowDemo", () => {
beforeEach(() => {
currentLocale = "en";
});

it("formats numbers and the token unit in the page locale", () => {
currentLocale = "ru";
render(<ContextWindowDemo />);

const tokens = bookMessages("ru").tokens as string;
const limit = (8000).toLocaleString("ru");

expect(screen.getByText(text(`${bookMessages("ru").contextWindow}: ${limit} ${tokens}`))).toBeInTheDocument();
expect(screen.getByText(text(`${(2000).toLocaleString("ru")} ${tokens}`))).toBeInTheDocument();
expect(screen.getByText(text(`${(1000).toLocaleString("ru")} ${tokens}`))).toBeInTheDocument();
expect(screen.queryByText(/\btokens\b/)).not.toBeInTheDocument();
});

it("does not depend on the runtime's default locale", () => {
currentLocale = "de";
render(<ContextWindowDemo />);

// German groups thousands with a dot, the en-US default with a comma.
expect(screen.getAllByText("8.000").length).toBeGreaterThan(0);
expect(screen.queryByText("8,000")).not.toBeInTheDocument();
});
});
13 changes: 7 additions & 6 deletions src/components/book/elements/demos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export function ContextWindowDemo() {
const [responseLength, setResponseLength] = useState(1000);
const contextLimit = 8000;
const t = useTranslations("book.interactive");
const locale = useLocale();

const totalUsed = promptLength + responseLength;
const remaining = Math.max(0, contextLimit - totalUsed);
Expand All @@ -150,12 +151,12 @@ export function ContextWindowDemo() {
<div className="p-4 space-y-6">
<div className="space-y-2">
<div className="flex items-center justify-between text-sm">
<span>{t("contextWindow")}: {contextLimit.toLocaleString()} tokens</span>
<span>{t("contextWindow")}: {contextLimit.toLocaleString(locale)} {t("tokens")}</span>
<span className={cn(
"font-mono",
isOverLimit ? "text-red-600 dark:text-red-400" : "text-green-600 dark:text-green-400"
)}>
{remaining.toLocaleString()} {t("remaining")}
{remaining.toLocaleString(locale)} {t("remaining")}
</span>
</div>
<div className="h-8 bg-muted rounded-lg overflow-hidden flex">
Expand All @@ -177,16 +178,16 @@ export function ContextWindowDemo() {
</div>
<div className="flex justify-between text-xs text-muted-foreground">
<span>0</span>
<span>{(contextLimit / 2).toLocaleString()}</span>
<span>{contextLimit.toLocaleString()}</span>
<span>{(contextLimit / 2).toLocaleString(locale)}</span>
<span>{contextLimit.toLocaleString(locale)}</span>
</div>
</div>

<div className="grid md:grid-cols-2 gap-4">
<div>
<label className="text-sm flex justify-between mb-1">
<span>{t("yourPrompt")}</span>
<span className="text-blue-600 dark:text-blue-400 font-mono">{promptLength.toLocaleString()} tokens</span>
<span className="text-blue-600 dark:text-blue-400 font-mono">{promptLength.toLocaleString(locale)} {t("tokens")}</span>
</label>
<input
type="range"
Expand All @@ -200,7 +201,7 @@ export function ContextWindowDemo() {
<div>
<label className="text-sm flex justify-between mb-1">
<span>{t("aiResponse")}</span>
<span className="text-green-600 dark:text-green-400 font-mono">{responseLength.toLocaleString()} tokens</span>
<span className="text-green-600 dark:text-green-400 font-mono">{responseLength.toLocaleString(locale)} {t("tokens")}</span>
</label>
<input
type="range"
Expand Down