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
65 changes: 65 additions & 0 deletions src/lib/updates/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { NoteBlock } from "@/lib/updates/notes";

/**
* Verbatim body of `quarry-alpha107`, read with `gh release view quarry-alpha107 --json body`.
* Kept as a fixture so parser and panel tests exercise the shape a release actually publishes:
* a summary line, two grouping headings, numbered entries with indented paragraphs, and an
* update paragraph with no entry above it.
*/
export const ALPHA107_BODY = [
"3 changes: notes with a summary, grouped entries and an update line; the initialize panel invites instead of diagnosing; let the What's new panel read the release it asks for.",
"",
"## Fixes",
"",
"- the initialize panel invites instead of diagnosing (#236)",
"",
' The panel had two screens — a button, then the picker — and the heading said **"This folder is not a project"** on both, so the second screen asked you to fill in a form for something it had just told you does not exist.',
"",
"- let the What's new panel read the release it asks for (#235)",
"",
' The panel said *"No release notes published for this version"* on a release that has notes, and `latest.json` carried them too.',
"",
"## Features",
"",
"- notes with a summary, grouped entries and an update line (#237)",
"",
" Published bodies were a bare `git log --pretty=format:%s` dump. The regression has a specific origin: `e37f6bd` built `notesFromGit` on commit subjects only, and `git log -S 'gh pr view' -- scripts` shows no code ever fetched PR bodies — the later refactors copied that shape faithfully.",
"",
"## How to update",
"",
"quarry self-updates; restart when a new version is offered. For a manual install, download quarry_0.1.0-alpha.107_universal.dmg from this release.",
].join("\n");

/** The four block kinds the fixture must produce, with inline markdown already removed. */
export const ALPHA107_BLOCKS: NoteBlock[] = [
{
kind: "lead",
text: "3 changes: notes with a summary, grouped entries and an update line; the initialize panel invites instead of diagnosing; let the What's new panel read the release it asks for.",
},
{ kind: "heading", text: "Fixes" },
{ kind: "entry", text: "the initialize panel invites instead of diagnosing", number: "236" },
{
kind: "body",
text: 'The panel had two screens — a button, then the picker — and the heading said "This folder is not a project" on both, so the second screen asked you to fill in a form for something it had just told you does not exist.',
},
{ kind: "entry", text: "let the What's new panel read the release it asks for", number: "235" },
{
kind: "body",
text: 'The panel said "No release notes published for this version" on a release that has notes, and latest.json carried them too.',
},
{ kind: "heading", text: "Features" },
{
kind: "entry",
text: "notes with a summary, grouped entries and an update line",
number: "237",
},
{
kind: "body",
text: "Published bodies were a bare git log --pretty=format:%s dump. The regression has a specific origin: e37f6bd built notesFromGit on commit subjects only, and git log -S 'gh pr view' -- scripts shows no code ever fetched PR bodies — the later refactors copied that shape faithfully.",
},
{ kind: "heading", text: "How to update" },
{
kind: "body",
text: "quarry self-updates; restart when a new version is offered. For a manual install, download quarry_0.1.0-alpha.107_universal.dmg from this release.",
},
];
4 changes: 4 additions & 0 deletions src/lib/updates/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** The three things the panel can say while notes are pending, empty or unreadable. */
export const NOTES_LOADING = "Loading notes…";
export const NOTES_EMPTY = "No release notes published for this version";
export const NOTES_FAILED = "Could not load release notes";
44 changes: 36 additions & 8 deletions src/lib/updates/notes.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, test } from "bun:test";
import { ALPHA107_BODY, ALPHA107_BLOCKS } from "@/lib/updates/fixtures";
import {
fetchReleaseNotes,
notesTagFor,
Expand All @@ -18,21 +19,38 @@ describe("notesTagFor", () => {
});

describe("parseNotes", () => {
test("headings, bullets and bold lose their markers", () => {
const body = ["## Changes", "", "- **feat**: restart lsp", "- fix: tree indent"].join("\n");
expect(parseNotes(body)).toEqual(["Changes", "feat: restart lsp", "fix: tree indent"]);
test("the alpha107 body becomes a lead, headings, entries and bodies", () => {
expect(parseNotes(ALPHA107_BODY)).toEqual(ALPHA107_BLOCKS);
});

test("inline code, bold, italic and a link lose their markers at parse time", () => {
const body = [
"## Changes",
"",
"- [restart](https://example.com) the `lsp` with **force** and *care* (#42)",
].join("\n");
expect(parseNotes(body)).toEqual([
{ kind: "heading", text: "Changes" },
{ kind: "entry", text: "restart the lsp with force and care", number: "42" },
]);
});

test("fences are dropped whole", () => {
const body = ["- one", "```", "code here", "```", "- two"].join("\n");
expect(parseNotes(body)).toEqual(["one", "two"]);
expect(parseNotes(body)).toEqual([
{ kind: "entry", text: "one", number: null },
{ kind: "entry", text: "two", number: null },
]);
});

test("numbered entries keep their text", () => {
expect(parseNotes("1. first\n2. second")).toEqual(["first", "second"]);
test("numbered items are entries without a PR number", () => {
expect(parseNotes("1. first\n2. second")).toEqual([
{ kind: "entry", text: "first", number: null },
{ kind: "entry", text: "second", number: null },
]);
});

test("an empty body yields no lines", () => {
test("an empty body yields no blocks", () => {
expect(parseNotes("\n\n \n")).toEqual([]);
});
});
Expand Down Expand Up @@ -71,7 +89,17 @@ describe("fetchReleaseNotes", () => {
return Promise.resolve(okBody);
});
expect(asked).toContain("/releases/tags/quarry-alpha77");
expect(notes).toEqual(["one", "two"]);
expect(notes).toEqual([
{ kind: "entry", text: "one", number: null },
{ kind: "entry", text: "two", number: null },
]);
});

test("an empty body is no notes, distinct from a failed lookup", async () => {
const notes = await fetchReleaseNotes("0.1.0-alpha.77", () =>
Promise.resolve({ ok: true, json: async () => ({ body: " \n\n" }) }),
);
expect(notes).toEqual([]);
});

test("a missing release is null, not a throw", async () => {
Expand Down
93 changes: 68 additions & 25 deletions src/lib/updates/notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
* Release notes: what changed in a version, taken from that version's GitHub release.
*
* The notes are written by `scripts/release-local.ts` from the commits since the previous tag, so this
* module only maps a version to its release tag, fetches the body once, and turns the markdown into the
* short list the panel draws. Nothing here renders markdown: the body is a commit list, and a line
* per entry is what makes it readable in a 300px panel.
* module only maps a version to its release tag, fetches the body once, and parses the markdown into
* blocks. The panel renders each block by kind, so structure survives from release body to screen.
*/

const REPOSITORY = "JeffreyJYZ/quarry";

/** One thing the panel draws. The kind is what decides its weight, dot and spacing. */
export type NoteBlock =
| { kind: "lead"; text: string }
| { kind: "heading"; text: string }
| { kind: "entry"; text: string; number: string | null }
| { kind: "body"; text: string };

/** `0.1.0-alpha.76` → `quarry-alpha76`, `1.2.3` → `v1.2.3`. */
export function notesTagFor(version: string): string {
const alpha = /^(\d+\.\d+\.\d+)-alpha\.(\d+)$/.exec(version);
Expand All @@ -17,44 +23,82 @@ export function notesTagFor(version: string): string {
}

/**
* The lines of a release body worth showing.
*
* Headings and bullets lose their markers, code fences and blank lines are dropped, and `**bold**`
* keeps its words. A body of only commit subjects therefore reads as a plain list of changes.
* Markdown that must not reach the DOM: code loses its backticks, emphasis loses its asterisks, and a
* link keeps only the words a reader needs. Done here so the panel can render plain text, never HTML.
*/
export function parseNotes(markdown: string): string[] {
const lines: string[] = [];
function stripInline(text: string): string {
return text
.replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
.replace(/`([^`]+)`/g, "$1")
.replace(/\*\*([^*]+)\*\*/g, "$1")
.replace(/\*([^*]+)\*/g, "$1");
}

const LEAD = /^\d+\s+changes?\b/i;
const BULLET = /^(?:[-*+]|\d+\.)\s+/;
const PR_NUMBER = /\s*\((?:#)?(\d+)\)$/;

/**
* A real release body: a summary line, `##` sections, `- title (#NNN)` entries and indented paragraphs.
* The first qualifying plain line is the lead; every later plain line is a body under the entry before
* it, or a loose body when no entry precedes it (the "How to update" paragraph).
*/
export function parseNotes(markdown: string): NoteBlock[] {
const blocks: NoteBlock[] = [];
let inFence = false;
let seenStructure = false;

for (const raw of markdown.split("\n")) {
const line = raw.trim();
if (line.startsWith("```")) {
if (raw.trim().startsWith("```")) {
inFence = !inFence;
continue;
}
if (inFence || line === "") continue;
const cleaned = line
.replace(/^#{1,6}\s*/, "")
.replace(/^[-*+]\s+/, "")
.replace(/^\d+\.\s+/, "")
.replace(/\*\*([^*]+)\*\*/g, "$1")
.trim();
if (cleaned === "") continue;
lines.push(cleaned);
if (inFence) continue;
const line = raw.trim();
if (line === "") continue;

if (/^#{1,6}\s+/.test(line)) {
blocks.push({ kind: "heading", text: stripInline(line.replace(/^#{1,6}\s+/, "")) });
seenStructure = true;
continue;
}

if (BULLET.test(line)) {
const text = stripInline(line.replace(BULLET, ""));
const match = PR_NUMBER.exec(text);
blocks.push({
kind: "entry",
text: match === null ? text : text.slice(0, match.index).trimEnd(),
number: match?.[1] ?? null,
});
seenStructure = true;
continue;
}

const text = stripInline(line);
if (blocks.length === 0 && !seenStructure && (LEAD.test(text) || text.endsWith(":"))) {
blocks.push({ kind: "lead", text });
continue;
}
blocks.push({ kind: "body", text });
}
return lines;
return blocks;
}

export type NotesFetch = (url: string) => Promise<{ ok: boolean; json: () => Promise<unknown> }>;

/** How long a release lookup may take before the panel is told it failed. */
export const NOTES_TIMEOUT_MS = 8000;

/** The notes for one version, or `null` when the release or its body cannot be read. */
/**
* The blocks for one version, `[]` for a release whose body has nothing to show, or `null` when the
* release or its body cannot be read. The panel says a different thing for the last two.
*/
export async function fetchReleaseNotes(
version: string,
fetchImpl: NotesFetch = fetch as unknown as NotesFetch,
timeoutMs: number = NOTES_TIMEOUT_MS,
): Promise<string[] | null> {
): Promise<NoteBlock[] | null> {
const tag = notesTagFor(version);
try {
const response = await withTimeout(
Expand All @@ -64,8 +108,7 @@ export async function fetchReleaseNotes(
if (!response.ok) return null;
const body = (await withTimeout(response.json(), timeoutMs)) as { body?: unknown };
if (typeof body.body !== "string") return null;
const notes = parseNotes(body.body);
return notes.length === 0 ? null : notes;
return parseNotes(body.body);
} catch {
return null;
}
Expand Down
78 changes: 78 additions & 0 deletions src/ui/updates/ReleaseNotes.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import "@/test/dom";
import { afterEach, describe, expect, mock, test } from "bun:test";
import { cleanup, render } from "@testing-library/react";
import { ALPHA107_BLOCKS } from "@/lib/updates/fixtures";
import { NOTES_EMPTY, NOTES_FAILED, NOTES_LOADING } from "@/lib/updates/messages";
import type { NoteBlock } from "@/lib/updates/notes";

const state: { notes: NoteBlock[] | null; failed: boolean } = {
notes: ALPHA107_BLOCKS,
failed: false,
};

mock.module("@/ui/updates/useReleaseNotes", () => ({ useReleaseNotes: () => state }));

import ReleaseNotes from "@/ui/updates/ReleaseNotes";

afterEach(() => {
state.notes = ALPHA107_BLOCKS;
state.failed = false;
cleanup();
});

describe("the what's new panel", () => {
test("headings and the lead are not bullets; entries are", () => {
const view = render(<ReleaseNotes version="0.1.0-alpha.107" />);
const items = [...view.container.querySelectorAll("li")];
expect(items).toHaveLength(3);
for (const item of items) expect(item.textContent).toMatch(/\(#\d+\)|#\d+/);

for (const text of ["3 changes:", "Fixes", "Features", "How to update"]) {
const node = [...view.container.querySelectorAll("p")].find((el) =>
el.textContent?.startsWith(text),
);
expect(node).toBeDefined();
expect(node?.closest("li")).toBeNull();
}
});

test("inline markdown never reaches the DOM", () => {
const view = render(<ReleaseNotes version="0.1.0-alpha.107" />);
const text = view.container.textContent ?? "";
expect(text).not.toContain("`");
expect(text).not.toContain("*");
expect(text).toContain("latest.json carried them too");
});

test("an entry carries its paragraph as a muted sibling", () => {
const view = render(<ReleaseNotes version="0.1.0-alpha.107" />);
const entry = [...view.container.querySelectorAll("li")].find((el) =>
el.textContent?.includes("the initialize panel invites"),
);
expect(entry).toBeDefined();
const title = entry?.querySelector(".notes-entry-title");
const paragraph = entry?.querySelector(".notes-entry-body");
expect(title?.textContent).toBe("the initialize panel invites instead of diagnosing");
expect(title?.parentElement).toBe(entry);
expect(paragraph?.parentElement).toBe(entry);
expect(paragraph?.textContent).toContain("This folder is not a project");
});

test("a failed lookup, an empty body and a pending one say different things", () => {
state.notes = null;
state.failed = true;
const failed = render(<ReleaseNotes version="0.1.0-alpha.107" />);
expect(failed.container.textContent).toContain(NOTES_FAILED);
cleanup();

state.notes = [];
state.failed = false;
const empty = render(<ReleaseNotes version="0.1.0-alpha.107" />);
expect(empty.container.textContent).toContain(NOTES_EMPTY);
cleanup();

state.notes = null;
const pending = render(<ReleaseNotes version="0.1.0-alpha.107" />);
expect(pending.container.textContent).toContain(NOTES_LOADING);
});
});
Loading
Loading