Skip to content
Myr

Ask for a why and its file's text at once on the why page

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- The page read the why, then the file's text at the why's commit. Live on dev-oscabriel the platform spent 4.8-6.9 s on a cold why and then 1.5-1.7 s on the text on a cold isolate, one after the other. - Both are asked for at once at the page's revision; should it have moved between the two (main landing in between), the text is read again at the why's commit, so the two still agree. - Tests (red first, why.test.tsx): both calls under way at once and one file read; a moved main reads the text again at the why's commit.

authored by oscabriel · committed by oscabriel · parent 4d45855 · Browse files · 2 files+131 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+131 added−11 removed

  • tests:+110 added−2 removed
  • other:+21 added−9 removed

2 files+131 added−11 removed

modified:apps/web/src/routes/$project/why/$rev/$.tsx+21 added-9 removed
modified:apps/web/test/why.test.tsx+110 added-2 removed
5 unmodified lines
6 * answer, its commit and its people; the browser's drawing is Pierre's7 * (`<diffs-container>`), loaded only there.8 */9import { describe, expect, it } from "@effect/vitest";10import { Client, Contracts } from "@repo/protocol/contracts";10import { CommitView, WhyView } from "@repo/protocol/domain/code";11import { CommitView, FileView, WhyView } from "@repo/protocol/domain/code";12import { LandingView } from "@repo/protocol/domain/landing";13import { Outcome } from "@repo/protocol/domain/outcome";14import { LandedSha, ProjectName } from "@repo/protocol/ids";14import { Schema } from "effect";15import { asTester, unanswered } from "@repo/protocol/testing";16import { Duration, Effect, Layer, Ref, Schema } from "effect";17import { RpcTest } from "effect/rpc";18import type { ReactElement } from "react";19import { renderToStaticMarkup } from "react-dom/server";2021import { LandingDetails } from "../src/features/why/range-summary.tsx";22import { WhyCode } from "../src/features/why/why-code.tsx";23import { WhyLines } from "../src/features/why/why-view.tsx";24import { readWhyPage } from "../src/routes/$project/why/$rev/$.tsx";2526const markupOf = (element: ReactElement) => renderToStaticMarkup(element);2728/** What a reader sees: the markup without its tags, entities decoded. */
278 unmodified lines
307 markupOf(<WhyCode project={datekit} text={TEXT} view={view} />)308 ).toContain("<diffs-container");309 });310});311312/** What the platform was asked by one page read: each `readFile`'s `at`, and the most calls under way at once. */313interface Asked {314 readonly files: readonly string[];315 readonly peak: number;316 readonly under: number;317}318319/**320 * A platform answering `why` with `view` and `readFile` with the file at321 * the commit `main` names (`mainAt`), each after a few milliseconds, every322 * call noted in `asked`.323 */324const whyPlatform = (asked: Ref.Ref<Asked>, mainAt: string) => {325 const during = <A, E>(answer: Effect.Effect<A, E>) =>326 Effect.acquireUseRelease(327 Ref.update(asked, (was) => ({328 ...was,329 peak: Math.max(was.peak, was.under + 1),330 under: was.under + 1,331 })),332 () => Effect.andThen(Effect.sleep(Duration.millis(30)), answer),333 () => Ref.update(asked, (was) => ({ ...was, under: was.under - 1 }))334 );335336 return Layer.effect(Client, RpcTest.makeClient(Contracts)).pipe(337 Layer.provide([338 Layer.mergeAll(339 unanswered,340 Contracts.toLayerHandler("why", () => during(Effect.succeed(view))),341 Contracts.toLayerHandler("readFile", (payload) =>342 during(343 Effect.gen(function* () {344 yield* Ref.update(asked, (was) => ({345 ...was,346 files: [...was.files, payload.at ?? "main"],347 }));348349 const at =350 payload.at === undefined || payload.at === "main"351 ? mainAt352 : payload.at;353354 return yield* Schema.decodeEffect(FileView)({355 at,356 binary: false,357 path: "src/format.ts",358 sha: "5".repeat(40),359 size: 6,360 text: `at ${at.slice(0, 4)}\n`,361 truncated: false,362 }).pipe(Effect.orDie);363 })364 )365 )366 ),367 asTester,368 ])369 );370};371372const nothingAsked: Asked = { files: [], peak: 0, under: 0 };373374const whyAddress = { _splat: "src/format.ts", project: "datekit", rev: "main" };375376describe("the why page's read", () => {377 it.live("asks for the why and the file's text at once", () =>378 Effect.gen(function* () {379 const asked = yield* Ref.make(nothingAsked);380381 const read = yield* readWhyPage(whyAddress).pipe(382 // A not-found is TanStack's own error, typed `any`: neither case383 // here reads one.384 Effect.orDie,385 Effect.provide(whyPlatform(asked, AT))386 );387388 expect(read.text).toBe("at aaaa\n");389 expect(yield* Ref.get(asked)).toStrictEqual({390 files: ["main"],391 peak: 2,392 under: 0,393 });394 })395 );396397 it.live(398 "reads the file again at the why's commit when main moved between the two reads",399 () =>400 Effect.gen(function* () {401 const asked = yield* Ref.make(nothingAsked);402403 const read = yield* readWhyPage(whyAddress).pipe(404 // A not-found is TanStack's own error, typed `any`: neither case405 // here reads one.406 Effect.orDie,407 Effect.provide(whyPlatform(asked, "b".repeat(40)))408 );409410 expect(read.text).toBe("at aaaa\n");411 expect((yield* Ref.get(asked)).files).toStrictEqual(["main", AT]);412 })413 );414});