Repository navigation
fix: pin the guide compiler and validate lesson output #1439
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
fhammerschmidt
wants to merge
3
commits into
rescript-lang:master
Choose a base branch
from
fhammerschmidt:fix/1421-guide-compiler
base: master
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
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
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,46 @@ | ||
| open Vitest | ||
|
|
||
| @module("./compilerLoadingFixture.js") | ||
| external interceptCompilerScripts: bool => unit = "interceptCompilerScripts" | ||
|
|
||
| @module("./compilerLoadingFixture.js") | ||
| external expireCompilerLoading: unit => unit = "expireCompilerLoading" | ||
|
|
||
| let renderWithCompiler = () => | ||
| render( | ||
| <ReactRouter.MemoryRouter> | ||
| <GuideHome | ||
| lessons=GuideTestFixtures.guideLessons | ||
| compilerData={bundleBaseUrl: "https://guide-compiler.test"} | ||
| /> | ||
| </ReactRouter.MemoryRouter>, | ||
| ) | ||
|
|
||
| test("shows a compiler loading failure in the guide output", async () => { | ||
| await viewport(1440, 900) | ||
| interceptCompilerScripts(true) | ||
| let screen = await renderWithCompiler() | ||
| await (await screen->getByText("Compiler setup failed"))->element->toBeVisible | ||
| await ( | ||
| await screen->getByText( | ||
| "Could not load compiler from url https://guide-compiler.test/v12.3.1/compiler.js", | ||
| ) | ||
| ) | ||
| ->element | ||
| ->toBeVisible | ||
| }) | ||
|
|
||
| test("shows a useful message when the compiler CDN never responds", async () => { | ||
| await viewport(1440, 900) | ||
| interceptCompilerScripts(false) | ||
| expireCompilerLoading() | ||
| let screen = await renderWithCompiler() | ||
| await (await screen->getByText("Compiler setup failed"))->element->toBeVisible | ||
| await ( | ||
| await screen->getByText( | ||
| "ReScript v12.3.1 did not load within 15 seconds. Reload the page to try again.", | ||
| ) | ||
| ) | ||
| ->element | ||
| ->toBeVisible | ||
| }) |
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,18 +1,8 @@ | ||
| open Vitest | ||
|
|
||
| test("selects the latest stable ReScript compiler with ESM output", async () => { | ||
| let version = | ||
| GuideCompilerSettings.latestStableVersion([ | ||
| "v12.1.0-alpha.1", | ||
| "v11.1.4", | ||
| "v12.0.0", | ||
| "v12.2.0-beta.1", | ||
| "v12.1.3", | ||
| ]) | ||
| ->Option.getOrThrow | ||
| ->Semver.toString | ||
|
|
||
| expect(version)->toBe("v12.1.3") | ||
| test("pins the guide to its validated v12 compiler with ESM output", async () => { | ||
| expect(GuideCompilerSettings.version)->toBe("v12.3.1") | ||
| expect(GuideCompilerSettings.parsedVersion->Semver.toString)->toBe(GuideCompilerSettings.version) | ||
| expect(GuideCompilerSettings.moduleSystem)->toBe("esmodule") | ||
| expect(GuideCompilerSettings.warnFlags->String.includes("-109"))->toBe(true) | ||
| }) |
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,193 @@ | ||
| import { beforeAll, afterEach, expect, test, vi } from "vitest"; | ||
| import { loadCompiler, runProgram } from "../../scripts/lesson-compiler.mjs"; | ||
| import * as Settings from "../../app/GuideCompilerSettings.jsx"; | ||
| import * as Validation from "../../app/GuideLessonValidation.jsx"; | ||
| import * as Content from "../../app/GuideLessonContent.jsx"; | ||
|
|
||
| const bundleBaseUrl = "https://cdn.rescript-lang.org"; | ||
| let compiler; | ||
| beforeAll(async () => { | ||
| compiler = await loadCompiler( | ||
| bundleBaseUrl, | ||
| Settings.version, | ||
| Settings.moduleSystem, | ||
| Settings.warnFlags, | ||
| ); | ||
| }, 60000); | ||
| afterEach(() => vi.unstubAllGlobals()); | ||
|
|
||
| function exercise(overrides = {}) { | ||
| return { | ||
| sourcePath: "test-lesson.mdx", | ||
| initialCode: 'let greeting = "hello"', | ||
| solutionCode: 'let greeting = "hello"', | ||
| expectedOutput: "hello", | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| test("the pinned browser compiler validates every real lesson", async () => { | ||
| expect(compiler.version).toBe(Settings.version.slice(1)); | ||
| await expect(Validation.validate(bundleBaseUrl)).resolves.toBeUndefined(); | ||
| const published = JSON.stringify(Content.load()); | ||
| expect(published).not.toContain("solutionCode"); | ||
| expect(published).not.toContain('greet(\\"Spock\\")'); | ||
| }); | ||
|
|
||
| test("invalid starter code fails even when its reference solution is valid", async () => { | ||
| await expect( | ||
| Validation.validateExercise( | ||
| compiler, | ||
| bundleBaseUrl, | ||
| exercise({ initialCode: "let x =" }), | ||
| ), | ||
| ).rejects.toThrow(/test-lesson\.mdx exercise\.initialCode does not compile/); | ||
| }); | ||
|
|
||
| test("invalid reference solutions identify the file and field", async () => { | ||
| await expect( | ||
| Validation.validateExercise( | ||
| compiler, | ||
| bundleBaseUrl, | ||
| exercise({ solutionCode: "let x: int = false" }), | ||
| ), | ||
| ).rejects.toThrow(/test-lesson\.mdx exercise\.solutionCode does not compile/); | ||
| }); | ||
|
|
||
| test("output mismatches fail with the expected and actual strings", async () => { | ||
| await expect( | ||
| Validation.validateExercise( | ||
| compiler, | ||
| bundleBaseUrl, | ||
| exercise({ expectedOutput: "hello " }), | ||
| ), | ||
| ).rejects.toThrow( | ||
| 'exercise.expectedOutput "hello " did not match solution output "hello"', | ||
| ); | ||
| }); | ||
|
|
||
| test("runtime failures identify the reference solution", async () => { | ||
| await expect( | ||
| Validation.validateExercise( | ||
| compiler, | ||
| bundleBaseUrl, | ||
| exercise({ solutionCode: 'JsError.throwWithMessage("boom")' }), | ||
| ), | ||
| ).rejects.toThrow( | ||
| /test-lesson\.mdx exercise\.solutionCode failed at runtime: boom/, | ||
| ); | ||
| }); | ||
|
|
||
| test("solutions execute against the pinned compiler's published runtime imports", async () => { | ||
| await expect( | ||
| Validation.validateExercise( | ||
| compiler, | ||
| bundleBaseUrl, | ||
| exercise({ | ||
| solutionCode: "let values = [1, 2]->Array.map(value => value + 1)", | ||
| expectedOutput: "[2,3]", | ||
| }), | ||
| ), | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| test("final expressions use the browser's source instrumentation", async () => { | ||
| await expect( | ||
| Validation.validateExercise( | ||
| compiler, | ||
| bundleBaseUrl, | ||
| exercise({ | ||
| solutionCode: 'let greeting = "starter"\nInt.toString(42)', | ||
| expectedOutput: "42", | ||
| }), | ||
| ), | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| test("multiple console arguments match the browser checkpoint serialization", async () => { | ||
| await expect( | ||
| Validation.validateExercise( | ||
| compiler, | ||
| bundleBaseUrl, | ||
| exercise({ | ||
| solutionCode: 'Console.log2("answer", 42)', | ||
| expectedOutput: "answer 42", | ||
| }), | ||
| ), | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| test("runaway solutions stop with a timeout", async () => { | ||
| await expect( | ||
| runProgram({ code: "while (true) {}", imports: {} }), | ||
| ).rejects.toThrow(/timed out/); | ||
| }); | ||
|
|
||
| test("missing compiler assets fail with the URL and HTTP status", async () => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn(async () => new Response("missing", { status: 404 })), | ||
| ); | ||
| await expect( | ||
| loadCompiler( | ||
| "https://missing-guide.test", | ||
| Settings.version, | ||
| Settings.moduleSystem, | ||
| Settings.warnFlags, | ||
| ), | ||
| ).rejects.toThrow( | ||
| `https://missing-guide.test/${Settings.version}/compiler.js: HTTP 404`, | ||
| ); | ||
| }); | ||
|
|
||
| test("a bundle claiming a different compiler version is rejected", async () => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn( | ||
| async (url) => | ||
| new Response( | ||
| url.endsWith("/compiler.js") | ||
| ? 'globalThis.rescript_compiler = {make: () => ({version: "13.0.0"})};' | ||
| : "", | ||
| ), | ||
| ), | ||
| ); | ||
| await expect( | ||
| loadCompiler( | ||
| "https://wrong-guide.test", | ||
| Settings.version, | ||
| Settings.moduleSystem, | ||
| Settings.warnFlags, | ||
| ), | ||
| ).rejects.toThrow( | ||
| `Expected guide compiler ${Settings.version}, received 13.0.0`, | ||
| ); | ||
| }); | ||
|
|
||
| test.each([undefined, 42, false])( | ||
| "reference validation rejects missing or non-string expected output: %s", | ||
| (expectedOutput) => { | ||
| const raw = `---\nexercise:\n initialCode: let x = 1\n${expectedOutput === undefined ? "" : ` expectedOutput: ${expectedOutput}\n`}---\n`; | ||
| expect(() => Validation.fromRaw(raw, "broken.mdx")).toThrow( | ||
| "broken.mdx exercise.expectedOutput must be a string", | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| test("reference code defaults to the starter and preserves an empty expected output", () => { | ||
| const parsed = Validation.fromRaw( | ||
| '---\nexercise:\n initialCode: let x = ""\n expectedOutput: ""\n---\n', | ||
| "empty.mdx", | ||
| ); | ||
| expect(parsed.solutionCode).toBe(parsed.initialCode); | ||
| expect(parsed.expectedOutput).toBe(""); | ||
| }); | ||
|
|
||
| test("empty reference code is an authoring error", () => { | ||
| expect(() => | ||
| Validation.fromRaw( | ||
| '---\nexercise:\n initialCode: let x = 1\n expectedOutput: "1"\n solutionCode: ""\n---\n', | ||
| "empty.mdx", | ||
| ), | ||
| ).toThrow("empty.mdx exercise.solutionCode must be a non-empty string"); | ||
| }); |
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,29 @@ | ||
| import { afterEach, vi } from "vitest"; | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| // Intercept script insertion rather than loading a remote compiler in UI failure tests. | ||
| export function interceptCompilerScripts(fail) { | ||
| const appendChild = document.body.appendChild.bind(document.body); | ||
| vi.spyOn(document.body, "appendChild").mockImplementation((node) => { | ||
| if ( | ||
| node instanceof HTMLScriptElement && | ||
| node.src.includes("/compiler.js") | ||
| ) { | ||
| if (fail) queueMicrotask(() => node.dispatchEvent(new Event("error"))); | ||
| return node; | ||
| } | ||
| return appendChild(node); | ||
| }); | ||
| } | ||
|
|
||
| export function expireCompilerLoading() { | ||
| // Advance only the guide's loading deadline; leave editor and test-library timers real. | ||
| const setTimeout = window.setTimeout.bind(window); | ||
| vi.spyOn(window, "setTimeout").mockImplementation( | ||
| (callback, delay, ...args) => | ||
| setTimeout(callback, delay === 15000 ? 0 : delay, ...args), | ||
| ); | ||
| } |
Oops, something went wrong.
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.
It should be possible to write this in ReScript.