diff --git a/AGENTS.md b/AGENTS.md index 17706edd9..c0cf89c48 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -38,8 +38,8 @@ relevant tests and the formatting/lint checks in CONTRIBUTING.md before stopping Report unavailable checks and failures accurately; a test summary followed by a hung process is a failure. -Update the existing page or documentation migration for changed behavior. Add a -migration only for new documentation content. +Update the existing documentation page or its authored source under +`examples/official-site/content/` for changed behavior. Build and preview the official site with the commands in its [README](./examples/official-site/README.md). Update `configuration.md` for configuration changes. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ee28efe42..83f901f02 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -111,7 +111,8 @@ Architectural constraints and documentation maintenance rules are in [AGENTS.md](./AGENTS.md). Edit existing documentation in the [official site](./examples/official-site/) in place. -Deployment recreates the website's database from the site's SQL migrations. +Deployment recreates the website's database from the generated documentation +seed and the site's checked-in migrations. Run `npm run site` to preview changes using the same site build as deployment. diff --git a/examples/official-site/README.md b/examples/official-site/README.md index 4445188e2..afa56dce5 100644 --- a/examples/official-site/README.md +++ b/examples/official-site/README.md @@ -4,7 +4,7 @@ The SQLPage website is of course built with SQLPage ! Things that you may be interested to look at: - The [custom component we use for our stylish home page](./sqlpage/templates/landing-page.handlebars) - - The [migrations](./sqlpage/migrations) that populate the database with the documentation for all components + - The [editable reference, examples, and articles](./content/) that populate the documentation - The [advanced multistep form example](./examples/multistep-form/) It is hosted as a simple Docker container on [sql-page.com](https://sql-page.com). @@ -21,6 +21,10 @@ Run SQLPage from that directory, or run `docker compose -f examples/official-site/docker-compose.yml up --build` from the repository root. +The builder writes one documentation migration and its search index. SQLPage +loads it into in-memory SQLite at startup, along with the checked-in demo and +installation migrations. + Add `theme=light` or `theme=dark` to a component reference URL to preview its live examples using SQLPage's stock theme. For example, visit `/component.sql?component=form&theme=light`. @@ -49,8 +53,8 @@ name. Keep the argument list in signature order and mark mandatory arguments wit Component pages in `content/components/` list parameters under `top` and `row`. Each `examples/*.md` file can contain `rows` in its YAML metadata. These rows drive both the live preview and the copyable SQL. The Markdown body is the -example caption. Use numbered filenames to keep the examples in order. -References that have not moved still come from the SQL migrations. +example caption. The numeric filename prefix is the public anchor: `04-embed.md` is `#example-4`. +Keep existing numbers when adding or removing examples. ## Landing page diff --git a/examples/official-site/component.sql b/examples/official-site/component.sql index 747882aab..bcb31e998 100644 --- a/examples/official-site/component.sql +++ b/examples/official-site/component.sql @@ -34,4 +34,4 @@ select 'dynamic' as component, json_array( case when result is not null then json_object('component', 'docs-example', 'end_result', true) else json_array() end ) as properties -from examples order by example_id; +from examples order by example_number; diff --git a/examples/official-site/content/categories.yaml b/examples/official-site/content/categories.yaml new file mode 100644 index 000000000..94861623f --- /dev/null +++ b/examples/official-site/content/categories.yaml @@ -0,0 +1,36 @@ +- id: display-data + title: Display data + sort_order: 1 +- id: forms-and-feedback + title: Forms and feedback + sort_order: 2 +- id: navigation + title: Navigation + sort_order: 3 +- id: page-layout + title: Page layout and content + sort_order: 4 +- id: http-responses + title: HTTP responses + sort_order: 5 +- id: server-logic + title: Server logic + sort_order: 6 +- id: request-data + title: Request data + sort_order: 1 +- id: links-and-routing + title: Links and routing + sort_order: 2 +- id: files-and-uploads + title: Files and uploads + sort_order: 3 +- id: identity-and-security + title: Identity and security + sort_order: 4 +- id: external-services + title: External services + sort_order: 5 +- id: server-and-runtime + title: Server and runtime + sort_order: 6 diff --git a/examples/official-site/sqlpage/migrations/01_documentation.sql b/examples/official-site/sqlpage/migrations/01_documentation.sql deleted file mode 100644 index 099d087be..000000000 --- a/examples/official-site/sqlpage/migrations/01_documentation.sql +++ /dev/null @@ -1,56 +0,0 @@ -CREATE TABLE component( - name TEXT PRIMARY KEY, - description TEXT NOT NULL, - icon TEXT, -- icon name from tabler icon - introduced_in_version TEXT -); - -CREATE TABLE parameter_type( - name TEXT PRIMARY KEY -); -INSERT INTO parameter_type(name) VALUES - ('BOOLEAN'), ('COLOR'), ('HTML'), ('ICON'), ('INTEGER'), ('JSON'), ('REAL'), ('TEXT'), ('TIMESTAMP'), ('URL'); - -CREATE TABLE parameter( - top_level BOOLEAN DEFAULT FALSE, - name TEXT, - component TEXT REFERENCES component(name) ON DELETE CASCADE, - description TEXT, - description_md TEXT, - type TEXT REFERENCES parameter_type(name) ON DELETE CASCADE, - optional BOOLEAN DEFAULT FALSE, - PRIMARY KEY (component, top_level, name) -); - -CREATE TABLE example( - component TEXT REFERENCES component(name) ON DELETE CASCADE, - description TEXT, - properties JSON, - FOREIGN KEY (component) REFERENCES component(name) ON DELETE CASCADE -); - --- The reference shows the SQL and renders the same stored component rows. --- Arrays with several items become repeated column aliases, as in handwritten SQLPage files. -CREATE VIEW example_with_source AS -SELECT example.rowid AS example_id, - row_number() OVER (PARTITION BY component ORDER BY example.rowid) AS example_number, - example.*, - (SELECT group_concat('select' || char(10) || ( - WITH fields AS ( - SELECT field.key AS name, coalesce(item.type, field.type) AS type, - CASE WHEN item.key IS NULL THEN field.value ELSE item.value END AS value - FROM json_each(statement.value) AS field - LEFT JOIN json_each( - CASE WHEN field.type = 'array' AND json_array_length(field.value) > 1 THEN field.value END - ) AS item ON true - ) - SELECT group_concat(' ' || CASE type - WHEN 'true' THEN 'TRUE' - WHEN 'false' THEN 'FALSE' - WHEN 'array' THEN 'JSON(' || quote(value) || ')' - WHEN 'object' THEN 'JSON(' || quote(value) || ')' - ELSE quote(value) - END || format(' as "%w"', name), ',' || char(10)) - FROM fields - ) || ';', char(10) || char(10)) FROM json_each(example.properties) AS statement) AS source -FROM example; diff --git a/examples/official-site/sqlpage/migrations/08_functions.sql b/examples/official-site/sqlpage/migrations/08_functions.sql deleted file mode 100644 index 90502d472..000000000 --- a/examples/official-site/sqlpage/migrations/08_functions.sql +++ /dev/null @@ -1,14 +0,0 @@ -CREATE TABLE IF NOT EXISTS sqlpage_functions ( - "name" TEXT PRIMARY KEY, - "icon" TEXT, - "description_md" TEXT, - "return_type" TEXT, - "introduced_in_version" TEXT -); -CREATE TABLE IF NOT EXISTS sqlpage_function_parameters ( - "function" TEXT REFERENCES sqlpage_functions("name"), - "index" INTEGER, - "name" TEXT, - "description_md" TEXT, - "type" TEXT -); diff --git a/examples/official-site/sqlpage/migrations/12_blog.sql b/examples/official-site/sqlpage/migrations/12_blog.sql deleted file mode 100644 index e5a13a442..000000000 --- a/examples/official-site/sqlpage/migrations/12_blog.sql +++ /dev/null @@ -1,8 +0,0 @@ -CREATE TABLE blog_posts ( - title TEXT PRIMARY KEY, - description TEXT NOT NULL, - icon TEXT NOT NULL, - external_url TEXT, - content TEXT, - created_at TIMESTAMP NOT NULL -); diff --git a/examples/official-site/sqlpage/migrations/31_card_docs_update.sql b/examples/official-site/sqlpage/migrations/31_card_docs_update.sql deleted file mode 100644 index 8da39f24f..000000000 --- a/examples/official-site/sqlpage/migrations/31_card_docs_update.sql +++ /dev/null @@ -1 +0,0 @@ -DELETE FROM component WHERE name = 'card'; diff --git a/examples/official-site/sqlpage/migrations/62_example_cards.sql b/examples/official-site/sqlpage/migrations/62_example_cards.sql deleted file mode 100644 index c13237d82..000000000 --- a/examples/official-site/sqlpage/migrations/62_example_cards.sql +++ /dev/null @@ -1 +0,0 @@ -CREATE TABLE example_cards (title TEXT NOT NULL, folder TEXT NOT NULL, db_engine TEXT, description TEXT NOT NULL); diff --git a/examples/official-site/sqlpage/migrations/81_guide_directory.sql b/examples/official-site/sqlpage/migrations/81_guide_directory.sql deleted file mode 100644 index e98e27a93..000000000 --- a/examples/official-site/sqlpage/migrations/81_guide_directory.sql +++ /dev/null @@ -1,6 +0,0 @@ -CREATE TABLE guide ( - sort_order INTEGER PRIMARY KEY, - title TEXT NOT NULL, - link TEXT NOT NULL UNIQUE, - description TEXT NOT NULL -); diff --git a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql deleted file mode 100644 index 615ed04e6..000000000 --- a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql +++ /dev/null @@ -1,69 +0,0 @@ --- Short catalog descriptions help readers choose a page before opening its full reference. -CREATE TABLE reference_category ( - id TEXT PRIMARY KEY, - title TEXT NOT NULL, - sort_order INTEGER NOT NULL -); - -INSERT INTO reference_category (id, title, sort_order) VALUES - ('display-data', 'Display data', 1), - ('forms-and-feedback', 'Forms and feedback', 2), - ('navigation', 'Navigation', 3), - ('page-layout', 'Page layout and content', 4), - ('http-responses', 'HTTP responses', 5), - ('server-logic', 'Server logic', 6), - ('request-data', 'Request data', 1), - ('links-and-routing', 'Links and routing', 2), - ('files-and-uploads', 'Files and uploads', 3), - ('identity-and-security', 'Identity and security', 4), - ('external-services', 'External services', 5), - ('server-and-runtime', 'Server and runtime', 6); - -ALTER TABLE component ADD COLUMN catalog_category TEXT REFERENCES reference_category(id); -ALTER TABLE component ADD COLUMN catalog_summary TEXT; -ALTER TABLE sqlpage_functions ADD COLUMN catalog_category TEXT REFERENCES reference_category(id); -ALTER TABLE sqlpage_functions ADD COLUMN catalog_summary TEXT; -ALTER TABLE sqlpage_function_parameters ADD COLUMN optional BOOLEAN NOT NULL DEFAULT FALSE; - --- The catalog, topic navigation, and reference pages use the same entries and links. -CREATE VIEW reference_entry AS -WITH entries AS ( - SELECT 'component' AS kind, name, icon, catalog_category, catalog_summary, - description AS description_md, introduced_in_version, NULL AS return_type, - '/component.sql?component=' || name AS link, - (SELECT count(*) FROM example WHERE example.component = component.name) AS example_count, - NULL AS arguments - FROM component - UNION ALL - SELECT 'function', name, icon, catalog_category, catalog_summary, - description_md, introduced_in_version, return_type, '/functions.sql?function=' || name, NULL, - coalesce((SELECT group_concat(CASE WHEN optional THEN '[' || name || ']' ELSE name END, ', ' ORDER BY "index") - FROM sqlpage_function_parameters WHERE "function" = f.name), '') - FROM sqlpage_functions AS f -) -SELECT kind, name, icon, link, description_md, introduced_in_version, return_type, example_count, arguments, - coalesce(category.id, 'other') AS category_id, - coalesce(category.title, 'Other') AS category_title, - coalesce(category.sort_order, 99) AS category_order, - coalesce(catalog_summary, substr(trim(description_md, char(10) || ' #'), 1, - instr(trim(description_md, char(10) || ' #') || char(10), char(10)) - 1)) AS summary -FROM entries -LEFT JOIN reference_category AS category ON category.id = entries.catalog_category; - --- The section is part of the anchor: a component can accept the same name at both levels. -CREATE VIEW reference_parameter AS -WITH fields AS ( - SELECT 'component' AS kind, component AS entry, - CASE WHEN top_level THEN 'top-level-parameters' ELSE 'row-level-parameters' END AS section, - name, type, optional, coalesce(description_md, description) AS description_md, optional AS position - FROM parameter - UNION ALL - SELECT 'function', "function", 'parameters', name, type, optional, description_md, "index" - FROM sqlpage_function_parameters -), sections(section, title, introduction, section_order) AS (VALUES - ('top-level-parameters', 'Top-level parameters', 'Set these in the SELECT that names the component.', 1), - ('row-level-parameters', 'Row-level parameters', 'Return these columns in the rows that follow the component.', 2), - ('parameters', 'Parameters', NULL, 1) -) -SELECT fields.*, title, introduction, section_order, section || '-' || name AS anchor -FROM fields JOIN sections USING (section); diff --git a/scripts/site/components.mjs b/scripts/site/components.mjs index 6939debfc..b81eccb50 100644 --- a/scripts/site/components.mjs +++ b/scripts/site/components.mjs @@ -55,7 +55,7 @@ export function insertComponents(seed, content) { } } }); - // A component may still have a SQL-authored reference and Markdown examples. + // The foreign key on example.component checks each directory against the catalog. const names = readdirSync(dir, { withFileTypes: true }) .filter((e) => e.isDirectory()) .map((e) => e.name) @@ -84,7 +84,12 @@ function orderedJson(value) { export function insertExamples(seed, name, dir) { pages( dir, - (_, meta, caption) => { + (file, meta, caption) => { + const number = Number(/^(\d+)-.+$/.exec(file)?.[1]); + if (!Number.isSafeInteger(number) || number < 1) + throw new Error( + "Expected a positive example number, such as 04-embed.md", + ); const { rows } = fields(meta, ["rows"]); if ( rows !== undefined && @@ -92,8 +97,9 @@ export function insertExamples(seed, name, dir) { ) throw new Error("Expected a list of rows"); seed.row( - "INSERT INTO example (component, description, properties) VALUES (?, ?, ?)", + "INSERT INTO example (component, example_number, description, properties) VALUES (?, ?, ?, ?)", name, + number, caption, rows === undefined ? null : orderedJson(rows), ); diff --git a/scripts/site/directories.mjs b/scripts/site/directories.mjs index a843bfe4c..20fbecdb2 100644 --- a/scripts/site/directories.mjs +++ b/scripts/site/directories.mjs @@ -3,6 +3,17 @@ import { resolve } from "node:path"; import { collection, fields, text } from "./content.mjs"; export function insertDirectories(seed, content) { + collection(content, "categories.yaml", (item) => { + const c = fields(item, ["id", "title", "sort_order"]); + if (!Number.isSafeInteger(c.sort_order)) + throw new Error("Expected an integer sort_order"); + seed.row( + "INSERT INTO reference_category (id, title, sort_order) VALUES (?, ?, ?)", + text(c.id, "id"), + text(c.title, "title"), + c.sort_order, + ); + }); collection(content, "guides.yaml", (item) => { const g = fields(item, ["sort_order", "title", "link", "description"]); if (!Number.isSafeInteger(g.sort_order)) diff --git a/scripts/site/documentation.mjs b/scripts/site/documentation.mjs index 2dc84c576..d906f4f22 100644 --- a/scripts/site/documentation.mjs +++ b/scripts/site/documentation.mjs @@ -1,4 +1,4 @@ -import { readdirSync, readFileSync, writeFileSync } from "node:fs"; +import { readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { DatabaseSync } from "node:sqlite"; import { insertComponents } from "./components.mjs"; @@ -58,27 +58,16 @@ export function insertPosts(seed, dir) { export function writeDocumentation(content, migrations) { const db = new DatabaseSync(":memory:"); try { - // The generated rows go after the old content tables and before the search index. - const version = 100; - const files = readdirSync(migrations) - .filter((f) => f.endsWith(".sql") && !f.endsWith(".down.sql")) - .sort((a, b) => Number.parseInt(a, 10) - Number.parseInt(b, 10)); - for (const file of files) { - const n = Number.parseInt(file, 10); - if (n === version) - throw new Error(`Migration ${version} is reserved: ${file}`); - if (n < version) db.exec(readFileSync(join(migrations, file), "utf8")); - } const seed = new Seed(db); + seed.script(readFileSync(new URL("./schema.sql", import.meta.url), "utf8")); insertDirectories(seed, content); insertPosts(seed, join(content, "blog")); insertComponents(seed, content); insertFunctions(seed, join(content, "functions")); - writeFileSync( - join(migrations, `${version}_documentation_content.sql`), - seed.toString(), - { flag: "wx" }, - ); + seed.script(readFileSync(new URL("./search.sql", import.meta.url), "utf8")); + writeFileSync(join(migrations, "01_documentation.sql"), seed.toString(), { + flag: "wx", + }); } finally { db.close(); } diff --git a/scripts/site/schema.sql b/scripts/site/schema.sql new file mode 100644 index 000000000..0f6b7ee9b --- /dev/null +++ b/scripts/site/schema.sql @@ -0,0 +1,147 @@ +CREATE TABLE reference_category ( + id TEXT PRIMARY KEY, + title TEXT NOT NULL, + sort_order INTEGER NOT NULL +); + +CREATE TABLE component ( + name TEXT PRIMARY KEY, + description TEXT NOT NULL, + icon TEXT, + introduced_in_version TEXT, + catalog_category TEXT REFERENCES reference_category(id), + catalog_summary TEXT +); + +CREATE TABLE parameter_type (name TEXT PRIMARY KEY); +INSERT INTO parameter_type VALUES + ('BOOLEAN'), ('COLOR'), ('HTML'), ('ICON'), ('INTEGER'), ('JSON'), ('REAL'), ('TEXT'), ('TIMESTAMP'), ('URL'); + +CREATE TABLE parameter ( + component TEXT NOT NULL REFERENCES component(name), + top_level BOOLEAN NOT NULL, + name TEXT NOT NULL, + type TEXT NOT NULL REFERENCES parameter_type(name), + description_md TEXT NOT NULL, + optional BOOLEAN NOT NULL, + PRIMARY KEY (component, top_level, name) +); + +CREATE TABLE example ( + component TEXT NOT NULL REFERENCES component(name), + example_number INTEGER NOT NULL CHECK (example_number > 0), + description TEXT NOT NULL, + properties JSON, + PRIMARY KEY (component, example_number) +); + +CREATE TABLE sqlpage_functions ( + name TEXT PRIMARY KEY, + icon TEXT, + description_md TEXT NOT NULL, + return_type TEXT, + introduced_in_version TEXT, + catalog_category TEXT REFERENCES reference_category(id), + catalog_summary TEXT +); + +CREATE TABLE sqlpage_function_parameters ( + "function" TEXT NOT NULL REFERENCES sqlpage_functions(name), + "index" INTEGER NOT NULL, + name TEXT NOT NULL, + description_md TEXT NOT NULL, + type TEXT NOT NULL, + optional BOOLEAN NOT NULL, + PRIMARY KEY ("function", "index"), + UNIQUE ("function", name) +); + +CREATE TABLE blog_posts ( + title TEXT PRIMARY KEY, + description TEXT NOT NULL, + icon TEXT NOT NULL, + external_url TEXT, + content TEXT, + created_at TEXT NOT NULL +); + +CREATE TABLE guide ( + sort_order INTEGER PRIMARY KEY, + title TEXT NOT NULL, + link TEXT NOT NULL UNIQUE, + description TEXT NOT NULL +); + +CREATE TABLE example_cards ( + title TEXT NOT NULL, + folder TEXT NOT NULL, + db_engine TEXT, + description TEXT NOT NULL +); + +-- The reference shows the SQL and renders the same stored component rows. +-- Arrays with several items become repeated column aliases, as in handwritten SQLPage files. +CREATE VIEW example_with_source AS +SELECT example.*, + (SELECT group_concat('select' || char(10) || ( + WITH fields AS ( + SELECT field.key AS name, coalesce(item.type, field.type) AS type, + CASE WHEN item.key IS NULL THEN field.value ELSE item.value END AS value + FROM json_each(statement.value) AS field + LEFT JOIN json_each( + CASE WHEN field.type = 'array' AND json_array_length(field.value) > 1 THEN field.value END + ) AS item ON true + ) + SELECT group_concat(' ' || CASE type + WHEN 'true' THEN 'TRUE' + WHEN 'false' THEN 'FALSE' + WHEN 'array' THEN 'JSON(' || quote(value) || ')' + WHEN 'object' THEN 'JSON(' || quote(value) || ')' + ELSE quote(value) + END || format(' as "%w"', name), ',' || char(10)) + FROM fields + ) || ';', char(10) || char(10)) FROM json_each(example.properties) AS statement) AS source +FROM example; + +-- The catalog, topic navigation, and reference pages use the same entries and links. +CREATE VIEW reference_entry AS +WITH entries AS ( + SELECT 'component' AS kind, name, icon, catalog_category, catalog_summary, + description AS description_md, introduced_in_version, NULL AS return_type, + '/component.sql?component=' || name AS link, + (SELECT count(*) FROM example WHERE example.component = component.name) AS example_count, + NULL AS arguments + FROM component + UNION ALL + SELECT 'function', name, icon, catalog_category, catalog_summary, + description_md, introduced_in_version, return_type, '/functions.sql?function=' || name, NULL, + coalesce((SELECT group_concat(CASE WHEN optional THEN '[' || name || ']' ELSE name END, ', ' ORDER BY "index") + FROM sqlpage_function_parameters WHERE "function" = f.name), '') + FROM sqlpage_functions AS f +) +SELECT kind, name, icon, link, description_md, introduced_in_version, return_type, example_count, arguments, + coalesce(category.id, 'other') AS category_id, + coalesce(category.title, 'Other') AS category_title, + coalesce(category.sort_order, 99) AS category_order, + coalesce(catalog_summary, substr(trim(description_md, char(10) || ' #'), 1, + instr(trim(description_md, char(10) || ' #') || char(10), char(10)) - 1)) AS summary +FROM entries +LEFT JOIN reference_category AS category ON category.id = entries.catalog_category; + +-- The section is part of the anchor: a component can accept the same name at both levels. +CREATE VIEW reference_parameter AS +WITH fields AS ( + SELECT 'component' AS kind, component AS entry, + CASE WHEN top_level THEN 'top-level-parameters' ELSE 'row-level-parameters' END AS section, + name, type, optional, description_md, optional AS position + FROM parameter + UNION ALL + SELECT 'function', "function", 'parameters', name, type, optional, description_md, "index" + FROM sqlpage_function_parameters +), sections(section, title, introduction, section_order) AS (VALUES + ('top-level-parameters', 'Top-level parameters', 'Set these in the SELECT that names the component.', 1), + ('row-level-parameters', 'Row-level parameters', 'Return these columns in the rows that follow the component.', 2), + ('parameters', 'Parameters', NULL, 1) +) +SELECT fields.*, title, introduction, section_order, section || '-' || name AS anchor +FROM fields JOIN sections USING (section); diff --git a/examples/official-site/sqlpage/migrations/999_fts_search_index.sql b/scripts/site/search.sql similarity index 100% rename from examples/official-site/sqlpage/migrations/999_fts_search_index.sql rename to scripts/site/search.sql diff --git a/tests/js/component_documentation.checks.ts b/tests/js/component_documentation.checks.ts index d8152250b..2825925b9 100644 --- a/tests/js/component_documentation.checks.ts +++ b/tests/js/component_documentation.checks.ts @@ -287,7 +287,7 @@ test("every property read by a template is documented", () => { undocumented.sort(), [], `These properties are read by a template but not documented. Add a row to ` + - `the component's migration in examples/official-site/sqlpage/migrations/.`, + `the component's reference in examples/official-site/content/components/.`, ); }); @@ -314,6 +314,6 @@ test("every documented property is read by its template", () => { unimplemented.sort(), [], `Remove the documentation, or fix its top_level, in ` + - `examples/official-site/sqlpage/migrations/.`, + `examples/official-site/content/components/.`, ); }); diff --git a/tests/site/components.test.mjs b/tests/site/components.test.mjs index 32a417d21..ff76d7763 100644 --- a/tests/site/components.test.mjs +++ b/tests/site/components.test.mjs @@ -1,5 +1,5 @@ import assert from "node:assert/strict"; -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { DatabaseSync } from "node:sqlite"; @@ -7,11 +7,11 @@ import test from "node:test"; import { insertExamples } from "../../scripts/site/components.mjs"; import { Seed } from "../../scripts/site/documentation.mjs"; -test("example SQL retains file and column order and large integer values", (t) => { +test("example links and copied SQL retain authored numbers, column order and large integers", (t) => { const dir = mkdtempSync(join(tmpdir(), "sqlpage-examples-")); t.after(() => rmSync(dir, { recursive: true, force: true })); writeFileSync( - join(dir, "02-result.md"), + join(dir, "04-result.md"), `--- rows: - component: text @@ -24,20 +24,40 @@ Result`, writeFileSync(join(dir, "01-notes.md"), "Just a note."); const db = new DatabaseSync(":memory:"); t.after(() => db.close()); - db.exec("CREATE TABLE example (component, description, properties)"); + db.exec( + readFileSync( + new URL("../../scripts/site/schema.sql", import.meta.url), + "utf8", + ), + ); + db.exec("INSERT INTO component (name, description) VALUES ('text', '')"); const seed = new Seed(db); insertExamples(seed, "text", dir); db.exec("DELETE FROM example"); db.exec(seed.toString()); const rows = db.prepare( - "SELECT description, properties FROM example ORDER BY rowid", + "SELECT example_number, description, properties FROM example_with_source ORDER BY example_number", ); rows.setReturnArrays(true); assert.deepEqual(rows.all(), [ - ["Just a note.", null], + [1, "Just a note.", null], [ + 4, "Result", `[{"component":"text","contents":"Reader's choice","2":9007199254740993,"1":42}]`, ], ]); + db.exec("DELETE FROM example WHERE example_number = 1"); + const { source } = db + .prepare( + "SELECT source FROM example_with_source WHERE component = 'text' AND example_number = 4", + ) + .get(); + assert.match(source, /9007199254740993 as "2",\s+42 as "1"/); + db.exec("DELETE FROM example"); + writeFileSync(join(dir, "04-collision.md"), "Another example."); + assert.throws( + () => insertExamples(seed, "text", dir), + /04-result\.md: UNIQUE constraint failed/, + ); }); diff --git a/tests/site/prepared-seed.test.mjs b/tests/site/prepared-seed.test.mjs new file mode 100644 index 000000000..9083b2bbb --- /dev/null +++ b/tests/site/prepared-seed.test.mjs @@ -0,0 +1,38 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { DatabaseSync } from "node:sqlite"; +import test from "node:test"; +import { writeDocumentation } from "../../scripts/site/documentation.mjs"; + +test("the published seed is reproducible and replays with searchable reference links", (t) => { + const root = resolve(import.meta.dirname, "../.."); + const dir = mkdtempSync(join(tmpdir(), "sqlpage-seed-")); + t.after(() => rmSync(dir, { recursive: true, force: true })); + writeDocumentation(join(root, "examples/official-site/content"), dir); + const seed = readFileSync( + join(root, "target/official-site/sqlpage/migrations/01_documentation.sql"), + "utf8", + ); + assert.ok( + seed === readFileSync(join(dir, "01_documentation.sql"), "utf8"), + "Rebuilding changed the seed", + ); + const db = new DatabaseSync(":memory:"); + t.after(() => db.close()); + db.exec(seed); + const search = + db.prepare(`SELECT page || '#' || fragment AS link FROM documentation_fts + WHERE documentation_fts MATCH ? AND title = ?`); + assert.equal( + search.get("embed", "table component: embed").link, + "/component.sql?component=table#top-level-parameters-embed", + ); + const { source } = db + .prepare( + "SELECT source FROM example_with_source WHERE component = 'table' AND example_number = 4", + ) + .get(); + assert.match(source, /'Chart' as "embed"/); +});