Files
STCourses/packages/feature-ai/test/web.test.tsx
T
Lucas WintherandClaude Opus 5.5 f3ce2e34bf fix(ai): lessonUrl builder in one place; authoring follows core canView
- Learner links go through lessonUrl/courseUrl (src/web/urls.ts).
- Generation, the studio course list and file listing require an authoring
  right and core canView, so a global author needs a grant for a restricted
  course. Contract requests 8 (global author in canView) and 9
  (STC_AI_ALLOWED_BASE_URLS in AppConfig).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-30 04:49:48 +01:00

47 lines
2.3 KiB
TypeScript

import { expect, test } from "bun:test";
import { renderToString } from "react-dom/server";
import { readNdjson } from "../src/web/api";
import { MentorBanner, Markdown } from "../src/web/components";
import { aiWeb } from "../src/web";
test("readNdjson splits events across chunk boundaries", async () => {
const text = '{"type":"meta","n":1}\n{"type":"text","text":"héllo"}\n{"type":"done"}\n';
const bytes = new TextEncoder().encode(text);
const stream = new ReadableStream<Uint8Array>({
start(c) { for (let i = 0; i < bytes.length; i += 5) c.enqueue(bytes.slice(i, i + 5)); c.close(); },
});
const out: unknown[] = [];
for await (const ev of readNdjson(stream)) out.push(ev);
expect(out).toEqual([{ type: "meta", n: 1 }, { type: "text", text: "héllo" }, { type: "done" }]);
});
test("the mentor banner says so plainly when mentor mode is off", () => {
expect(renderToString(<MentorBanner on={false} available />)).toContain("Mentor mode off: I&#x27;ll give full answers");
expect(renderToString(<MentorBanner on available />)).toBe("");
expect(renderToString(<MentorBanner on={false} available={false} />)).toBe("");
});
test("markdown renders code and text without raw HTML", () => {
const html = renderToString(<Markdown text={"Use **this**:\n\n```js\nconst a = 1 < 2\n```\n\n- one `x`\n- <b>two</b>"} />);
expect(html).toContain("<strong>this</strong>");
expect(html).toContain('<pre class="code">');
expect(html).toContain("const a = 1 &lt; 2");
expect(html).toContain("<code>x</code>");
expect(html).toContain("&lt;b&gt;two&lt;/b&gt;");
});
test("web routes", () => {
expect(aiWeb.routes.map((r) => r.path)).toEqual(["/c/:courseId/assistant", "/admin/ai", "/account/ai", "/studio/generate"]);
expect(aiWeb.routes.find((r) => r.path === "/admin/ai")!.nav).toEqual({ label: "AI", area: "admin" });
});
test("lesson links go through one builder", async () => {
const { lessonUrl, courseUrl } = await import("../src/web/urls");
expect(lessonUrl("sample", "m2-ex-add")).toBe("/c/sample/m2-ex-add");
expect(lessonUrl("S08 - Docker", "l 1/x")).toBe("/c/S08%20-%20Docker/l%201%2Fx");
expect(courseUrl("sample")).toBe("/c/sample");
const src = await Bun.file(new URL("../src/web/AssistantPage.tsx", import.meta.url)).text();
expect(src).toContain("lessonUrl(courseId, c.lessonId)");
expect(src).not.toMatch(/`\/c\//);
});