From 816344d7246de2f805428ce3f3da112e8e650cb2 Mon Sep 17 00:00:00 2001 From: "g. nicholas d'andrea" Date: Fri, 3 Jul 2026 17:42:28 -0400 Subject: [PATCH] web/bugc-react: add curated example selector to the docs BugPlayground MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The docs BugPlayground only ever showed one program; the example picker lived in the standalone packages/playground app. Port a lean version into the docs widget (gnidan #33). - bugc-react: add a curated `bugExamples` set (Counter, Function calls, Arrays & loops) as a reusable export. Sources are distilled from packages/bugc/examples and stripped of the `/*@test*/` behavioral annotations the raw files carry, so they read cleanly in an editor. examples.test.ts asserts each compiles to bytecode without errors (guards against a typo in an inlined source); adds the missing vitest.config.ts so the package's test script runs. - web BugPlayground: add an optional `examples` prop (defaults to bugExamples) and an Example dropdown in the header controls. The selector shows only when no `initialCode` is passed — so the bare playground page gains it while inline single-example doc embeds stay unchanged. Selecting an example loads its source and recompiles. Additive; does not affect the tracer or #227. Example sources verified to compile via the new tests; UI wiring mirrors the existing optimizer selector. --- packages/bugc-react/src/examples.test.ts | 44 ++++++++ packages/bugc-react/src/examples.ts | 102 ++++++++++++++++++ packages/bugc-react/src/index.ts | 3 + packages/bugc-react/vitest.config.ts | 13 +++ .../src/theme/BugcExample/BugPlayground.css | 6 +- .../src/theme/BugcExample/BugPlayground.tsx | 90 +++++++++++----- 6 files changed, 228 insertions(+), 30 deletions(-) create mode 100644 packages/bugc-react/src/examples.test.ts create mode 100644 packages/bugc-react/src/examples.ts create mode 100644 packages/bugc-react/vitest.config.ts diff --git a/packages/bugc-react/src/examples.test.ts b/packages/bugc-react/src/examples.test.ts new file mode 100644 index 000000000..97ae456af --- /dev/null +++ b/packages/bugc-react/src/examples.test.ts @@ -0,0 +1,44 @@ +/** + * The curated example set must stay small, clean, and — above all — + * compilable: these strings are shipped verbatim into the docs + * playground editor, so a typo would surface as a broken example. + */ +import { describe, it, expect } from "vitest"; +import { compile } from "@ethdebug/bugc"; +import { bugExamples } from "./examples.js"; + +describe("bugExamples", () => { + it("is the curated trio (counter, functions, arrays)", () => { + expect(bugExamples.map((e) => e.name)).toEqual([ + "counter", + "functions", + "arrays", + ]); + }); + + it("gives every example a display name and non-empty source", () => { + for (const ex of bugExamples) { + expect(ex.displayName.trim().length).toBeGreaterThan(0); + expect(ex.code.trim().length).toBeGreaterThan(0); + } + }); + + it("carries no leftover @test annotation blocks", () => { + for (const ex of bugExamples) { + expect(ex.code).not.toContain("@test"); + } + }); + + // Each curated source must compile cleanly to bytecode — this is + // the guard that matters, since these ship straight to the editor. + for (const ex of bugExamples) { + it(`compiles ${ex.name} to bytecode without errors`, async () => { + const result = await compile({ + to: "bytecode", + source: ex.code, + optimizer: { level: 0 }, + }); + expect(result.success).toBe(true); + }); + } +}); diff --git a/packages/bugc-react/src/examples.ts b/packages/bugc-react/src/examples.ts new file mode 100644 index 000000000..99c1dcbd9 --- /dev/null +++ b/packages/bugc-react/src/examples.ts @@ -0,0 +1,102 @@ +/** + * A small, curated set of BUG programs for the docs playground's + * example selector. Kept intentionally short and clean — these + * strings are shown verbatim in the editor, so they carry no + * `/*@test*\/`-style behavioral annotations (unlike the raw + * `packages/bugc/examples` sources they're distilled from). + * + * The set is validated by examples.test.ts: every entry must + * compile to bytecode without errors. + */ + +/** A named, display-labelled BUG source for the example selector. */ +export interface BugExample { + /** Stable identifier (used as the