/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ import assert from "node:assert/strict"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { serializeNodes } from "../src/processorProfiles.js"; import { createTransformRegistry } from "../src/transformRegistry.js"; const testDirectory = path.dirname(fileURLToPath(import.meta.url)); async function generate( transformName: string, options: Record, ): Promise { const registry = createTransformRegistry(); const transform = registry.transforms[transformName]; assert(transform !== undefined); const nodes = await transform.generate( options, registry.createContext(path.join(testDirectory, "fixture.md"), "markdown", 2), ); return serializeNodes(nodes, "markdown"); } test("library header applies public package defaults", async () => { const output = await generate("library-readme-header", { packageJsonPath: "./package.json", devDependency: true, packageScopeNotice: "EXPERIMENTAL", }); assert.match(output, /^\*\*IMPORTANT: This package is experimental\.\*\*/); assert.match(output, /## Using Fluid Framework libraries/); assert.match(output, /## Installation\n\nRun this command/); assert.match(output, /npm i @fluidframework\/test-package -D/); assert.match(output, /## Importing from this package/); assert.match(output, /@fluidframework\/test-package\/alpha/); assert.match(output, /## API Documentation/); }); test("README footer rejects the removed scripts option", async () => { await assert.rejects( generate("readme-footer", { scripts: true }), /Unknown option "scripts" for transform "readme-footer"/, ); }); test("package scripts include optional descriptions in a separate column", async () => { const output = await generate("package-scripts", { packageJsonPath: "./package.json", scriptDescriptions: { "test-script-1": "Runs the **first** test script.", }, }); assert.match(output, /\| Script Name \| Script Body \| Description \|/); assert.match( output, /\| `test-script-1` \| `echo This is a test script` \| Runs the \*\*first\*\* test script\. \|/, ); assert.match(output, /\| `test-script-2` \| `echo This is another test script` \|\s*\|/); }); test("package scripts omit the description column when no descriptions are supplied", async () => { const output = await generate("package-scripts", { packageJsonPath: "./package.json", }); assert.match(output, /\| Script Name \| Script Body \|/); assert.doesNotMatch(output, /Description/); assert.match(output, /\| `test-script-1` \| `echo This is a test script` \|/); }); test("package scripts reject non-string descriptions", async () => { await assert.rejects( generate("package-scripts", { scriptDescriptions: { test: true }, }), /Option "scriptDescriptions" for "package-scripts" must be a record of strings/, ); }); test("package scripts reject descriptions for unknown scripts", async () => { await assert.rejects( generate("package-scripts", { scriptDescriptions: { unknown: "Unknown script." }, }), /Description provided for unknown script "unknown"/, ); }); test("template transforms apply heading options structurally", async () => { const output = await generate("installation-instructions", { packageJsonPath: "./package.json", includeHeading: false, devDependency: true, }); assert.doesNotMatch(output, /Installation/); assert.match(output, /npm i @fluidframework\/test-package -D/); }); test("framework package scope produces no notice", async () => { const output = await generate("package-scope-notice", { packageJsonPath: "./package.json", scopeKind: "FRAMEWORK", }); assert.equal(output, ""); }); test("example app header honors the Tinylicious option", async () => { const output = await generate("example-app-readme-header", { packageJsonPath: "./package.json", usesTinylicious: false, }); assert.match(output, /^## Getting Started/); assert.doesNotMatch(output, /start a Tinylicious server/); assert.match(output, /pnpm start/); }); test("ServiceClient instructions honor the Tinylicious option", async () => { const output = await generate("example-getting-started", { packageJsonPath: "./package.json", serviceClient: true, usesTinylicious: false, }); assert.match(output, /fluidClient=session/); assert.doesNotMatch(output, /fluidClient=tinylicious/); }); test("transform options reject unknown properties", async () => { const registry = createTransformRegistry(); const transform = registry.transforms["library-readme-header"]; assert(transform !== undefined); await assert.rejects( async () => transform.generate( { scripts: true }, registry.createContext(path.join(testDirectory, "fixture.md"), "markdown", 2), ), /Unknown option "scripts" for transform "library-readme-header"/, ); }); test("transform options reject null values", async () => { const registry = createTransformRegistry(); const transform = registry.transforms["readme-footer"]; assert(transform !== undefined); await assert.rejects( async () => transform.generate( { help: null }, registry.createContext(path.join(testDirectory, "fixture.md"), "markdown", 2), ), /Option "help" for "readme-footer" must be a boolean/, ); }); test("section transforms use the explicit heading level", async () => { const registry = createTransformRegistry(); for (const transformName of ["api-docs", "installation-instructions", "package-scripts"]) { const transform = registry.transforms[transformName]; assert(transform !== undefined); const nodes = await transform.generate( { headingLevel: 2 }, registry.createContext(path.join(testDirectory, "fixture.md"), "markdown", 4), ); assert.equal(nodes[0]?.type, "heading"); assert.equal(nodes[0].depth, 2); } });