Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/github-draft-body-whole.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@schlessera/brain-ui-kit": patch
---

A not-posted GitHub draft now shows its whole body (#4197). `GitHubPullRequest` in `preview` cut a draft longer than 20,000 characters and said "Truncated · the rest is on GitHub", though a draft is not on GitHub yet and the host posts up to 65,536 characters. A draft keeps its 8-line clamp, whose expand control names the whole length. Read bodies keep their 20,000-character bound and their "Truncated" note. The W1 issue approval (`GitHubIssue` with `posting: { status: "preview" }`) already showed the whole body, and a unit test and a long-draft story now hold it to that.
13 changes: 10 additions & 3 deletions packages/ui-kit/src/github/object-parts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Field, GitHubLink, githubHref, type GitHubState } from "./shared.js";

/** Internal pieces shared by `GitHubPullRequest` and `GitHubIssue`. */

/** The body's hard bound, from the design's truncation table. */
const BODY_MAX_CHARS = 20_000;
/** A read or posted body's hard bound, from the design's truncation table. */
export const BODY_MAX_CHARS = 20_000;

/** "1,840": a count in the reader-facing format, the same in every locale the kit renders in. */
export function count(n: number): string {
Expand Down Expand Up @@ -68,6 +68,13 @@ export function Labels({ labels }: { labels: string[] }) {
/** Where a not-posted draft's links say they come from: it is not on GitHub yet. */
export const DRAFT_SOURCE = { source: "this draft", sourcePhrase: "in this draft" } as const;

/**
* An issue or pull request body. A read or posted body keeps the design's
* 20,000-character bound and its "Truncated" note. A draft (`draft`, a
* not-posted preview) shows its whole text behind the same clamp: what is
* approved is what is posted, up to 65,536 characters (#4197, as #4154 did
* for the W4 comment preview).
*/
export function Body(p: { body?: string | null; author: string; bodyLength?: number; bodyTruncated?: boolean; htmlUrl?: string | null; draft?: boolean }) {
if (!p.body) {
return <p style={{ margin: 0, font: `400 12px/1.5 ${font.body}`, color: color.inkMute }}>— No description provided.</p>;
Expand All @@ -81,7 +88,7 @@ export function Body(p: { body?: string | null; author: string; bodyLength?: num
lines={8}
linesNarrow={10}
expandLabel={`Show full description · ${count(length)} characters`}
maxChars={BODY_MAX_CHARS}
{...(p.draft ? {} : { maxChars: BODY_MAX_CHARS })}
truncated={p.bodyTruncated}
truncatedNote={
!p.draft && githubHref(p.htmlUrl) ? (
Expand Down
21 changes: 20 additions & 1 deletion packages/ui-kit/stories/github/GitHubPullRequest.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import preview from "#.storybook/preview";
import { useState } from "react";
import { expect, within } from "storybook/test";
import { expect, userEvent, within } from "storybook/test";

import { argos, commits, eurycleia, penelope, pr, prThread, telemachus } from "../../fixtures/github.js";
import { GitHubCommitRow } from "../../src/github/GitHubCommitRow.js";
Expand Down Expand Up @@ -168,3 +168,22 @@ export const Preview = meta.story({
},
});
export const PreviewNarrow = Preview.extend({ ...narrow });

/**
* A preview shows its whole draft (#4197): the read bodies' 20,000-character
* bound does not cut what is approved. The 8-line clamp stays, and its
* expand control names the whole length.
*/
const LONG_PR_DRAFT = `${"The key log gains a second signature per opening. ".repeat(500)}The last line of the draft.`;
export const PreviewLongDraft = meta.story({
args: { preview: true, body: LONG_PR_DRAFT, bodyLength: undefined, bodyTruncated: false },
...desktop,
play: async ({ canvas }) => {
await expect(canvas.queryByText(/Truncated/), "a draft is never shown cut").toBeNull();
const length = Array.from(LONG_PR_DRAFT).length;
await expect(length).toBeGreaterThan(20_000);
await userEvent.click(canvas.getByRole("button", { name: `Show full description · ${length.toLocaleString("en-US")} characters` }));
await expect(canvas.getByText(/The last line of the draft\./)).toBeVisible();
},
});
export const PreviewLongDraftNarrow = PreviewLongDraft.extend({ ...narrow });
25 changes: 24 additions & 1 deletion packages/ui-kit/stories/github/GitHubWrite.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import preview from "#.storybook/preview";
import { expect, fn } from "storybook/test";
import { expect, fn, userEvent } from "storybook/test";

import { brainBot } from "../../fixtures/github.js";
import { ApprovalCard } from "../../src/decisions/ApprovalCard.js";
Expand Down Expand Up @@ -49,6 +49,29 @@ export const W1IssueApproval = meta.story({
});
export const W1IssueApprovalNarrow = W1IssueApproval.extend({ ...narrow });

/**
* W1 with a draft past the read bodies' 20,000-character bound (#4197): the
* card shows all of what is approved, in both panes, and never says
* "Truncated". The host posts up to 65,536 characters.
*/
const LONG_ISSUE_DRAFT = `${"The tally of the wine store, night by night. ".repeat(560)}The last line of the draft.`;
export const W1LongDraft = meta.story({
...desktop,
render: () => (
<ApprovalCard tool="Post to GitHub?" target="ithaca/hall" toolIcon="issue-open" badge="Can't be undone" diff="" risk="" wrapHeader allowLabel="Post issue" denyLabel="Don't post" onAllow={fn()} onDeny={fn()}>
<GitHubIssue state="open" repo="ithaca/hall" title="The wine store tally, night by night" body={LONG_ISSUE_DRAFT} posting={{ status: "preview" }} />
</ApprovalCard>
),
play: async ({ canvas }) => {
await expect(Array.from(LONG_ISSUE_DRAFT).length).toBeGreaterThan(20_000);
await expect(canvas.queryByText(/Truncated/), "a draft is never shown cut").toBeNull();
await expect(canvas.getByText(/The last line of the draft\./), "As shown renders the whole draft").toBeVisible();
await userEvent.click(canvas.getByRole("tab", { name: "Exact text" }));
await expect(canvas.getByText(/The last line of the draft\.$/), "Exact text is the whole string").toBeVisible();
},
});
export const W1LongDraftNarrow = W1LongDraft.extend({ ...narrow });

/** W2: the same slot, now the real object, posted. */
export const W2Posted = meta.story({
...desktop,
Expand Down
62 changes: 62 additions & 0 deletions packages/ui-kit/tests/github-draft-body.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* A not-posted draft's body is never cut (#4197): what the user approves is
* what the host posts, up to 65,536 characters, so the read bodies'
* 20,000-character bound does not apply to it. As markup; the 320px layout,
* both themes and the clamp's expand control in a browser are the W1 long
* draft stories' in `stories/github/GitHubWrite.stories.tsx`.
*/
import { describe, expect, test } from "bun:test";
import { Window } from "happy-dom";
import type { ReactElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";

import { telemachus } from "../fixtures/github.js";
import { GitHubIssue } from "../src/github/GitHubIssue.js";
import { GitHubPullRequest } from "../src/github/GitHubPullRequest.js";
import { BODY_MAX_CHARS } from "../src/github/object-parts.js";

function draw(element: ReactElement): Document {
const { document } = new Window();
document.body.innerHTML = renderToStaticMarkup(element);
return document as unknown as Document;
}

/** Past the read bound, and well inside the write limit. */
const LONG = `${"The tally of the wine store, night by night. ".repeat(Math.ceil((BODY_MAX_CHARS + 5_000) / 45))}THE-LAST-LINE`;

const pr = (p: { preview?: boolean }) => (
<GitHubPullRequest state="open" repo="ithaca/hall" number={412} title="Sign the cellar key log" author={telemachus} headRef="key-log" baseRef="main" reviews={null} checks={null} body={LONG} {...p} />
);

describe("a draft body", () => {
test("is longer than the read bound in this test", () => {
expect(Array.from(LONG).length).toBeGreaterThan(BODY_MAX_CHARS + 1_000);
});

test("the W1 issue preview renders all of it, with no Truncated note", () => {
const doc = draw(<GitHubIssue state="open" repo="ithaca/hall" title="Cellar key log is not signed after dusk" body={LONG} posting={{ status: "preview" }} />);
expect(doc.body.textContent, "W1_PREVIEW_WHOLE_BODY").toContain("THE-LAST-LINE");
expect(doc.body.textContent, "W1_PREVIEW_NO_TRUNCATED_NOTE").not.toContain("Truncated");
});

test("a pull request preview renders all of it, with no Truncated note", () => {
const doc = draw(pr({ preview: true }));
expect(doc.body.textContent, "PR_PREVIEW_WHOLE_BODY").toContain("THE-LAST-LINE");
expect(doc.body.textContent, "PR_PREVIEW_NO_TRUNCATED_NOTE").not.toContain("Truncated");
expect(doc.body.textContent, "a draft is not on GitHub").not.toContain("rest is on GitHub");
});
});

describe("a read body keeps its bound", () => {
test("an issue as read is cut at the bound and says so", () => {
const doc = draw(<GitHubIssue state="open" repo="ithaca/hall" number={388} title="Suitors keep draining the wine store" author={telemachus} body={LONG} />);
expect(doc.body.textContent, "ISSUE_READ_BOUND").not.toContain("THE-LAST-LINE");
expect(doc.body.textContent, "ISSUE_READ_TRUNCATED_NOTE").toContain("Truncated");
});

test("a pull request as read is cut at the bound and says so", () => {
const doc = draw(pr({}));
expect(doc.body.textContent, "PR_READ_BOUND").not.toContain("THE-LAST-LINE");
expect(doc.body.textContent, "PR_READ_TRUNCATED_NOTE").toContain("Truncated");
});
});
Loading