Skip to content
Open
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
87 changes: 47 additions & 40 deletions website/__tests__/lib/mock/conformance/competition-versions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { describe, it, expect } from "vitest";
import yaml from "js-yaml";
import type { Version } from "@/lib/mock/data/fixtures";
import { resolveSchemaRefs } from "@/lib/schema/ref-resolver";
import { schemaFilePath } from "@/lib/schema/schema-files";
import { getValidator } from "./schema-validator";

const HERE = path.dirname(fileURLToPath(import.meta.url));
Expand Down Expand Up @@ -319,79 +320,85 @@ describe("the unversioned CompetitionBase authoring schema", () => {
// authoring schema, whose references resolve where it is served.
describe("the Form and Competition docs pages", () => {
const REPO_ROOT = path.resolve(HERE, "../../../../..");
const page = readFileSync(
path.join(REPO_ROOT, "website/src/content/docs/protocol/models/form.mdx"),
"utf-8",
);
const readPage = (model: string) =>
readFileSync(
path.join(
REPO_ROOT,
`website/src/content/docs/protocol/models/${model}.mdx`,
),
"utf-8",
);
const page = readPage("form");
const { form } = yaml.load(page.split("---")[1]) as {
form: {
example: { code: string };
table: { file: { path: string } };
jsonSchema: { file: { path: string } };
};
form: { example: { code: string } };
};
const load = (repoPath: string) =>
yaml.load(readFileSync(path.join(REPO_ROOT, repoPath), "utf-8")) as Schema;

/**
* The files a page's table and JSON Schema tab read for a schema. A
* `<SchemaTable>` with a `version` reads that version's copy; the
* `<SchemaFormatTabs>` JSON Schema tab reads the unversioned file.
*/
const filesFor = (pageText: string, schema: string) => {
const tag = (component: string) =>
pageText.match(
new RegExp(`<${component}\\b[^>]*\\bschema="${schema}"[^>]*>`),
)?.[0];
const table = tag("SchemaTable");
const tabs = tag("SchemaFormatTabs");
expect(table, `a SchemaTable for ${schema}`).toBeDefined();
expect(tabs, `a SchemaFormatTabs for ${schema}`).toBeDefined();
expect(
tabs,
"the tabs take their JSON Schema from the schema file",
).not.toMatch(/\bjsonSchema=/);
const version = table!.match(/\bversion="([^"]+)"/)?.[1];
return {
table: version ? schemaFilePath(schema, version) : schemaFilePath(schema),
jsonSchema: schemaFilePath(schema),
};
};
const formFiles = filesFor(page, "FormBase");

it("shows exactly the v0.5 FormBase example", () => {
expect(JSON.parse(form.example.code)).toEqual(
exampleOf(requireSchema("jsonSchema", "0.5.0", "FormBase")),
);
});

it("renders the table from the v0.5 schema, which requires title alone", () => {
expect(page).toContain(
"<SchemaTable filePath={frontmatter.form.table.file.path} />",
);
expect(form.table.file.path).toBe(
expect(formFiles.table).toBe(
"website/public/schemas/yaml/versions/v0.5.0/FormBase.yaml",
);
const table = load(form.table.file.path);
const table = load(formFiles.table);
expect(table.required).toContain("title");
expect(table.required).not.toContain("name");
});

it("renders the Competition table from the v0.5 schema, which requires opportunity alone", () => {
const competitionPage = readFileSync(
path.join(
REPO_ROOT,
"website/src/content/docs/protocol/models/competition.mdx",
),
"utf-8",
);
const { competition } = yaml.load(competitionPage.split("---")[1]) as {
competition: { table: { file: { path: string } } };
};
expect(competitionPage).toContain(
"<SchemaTable filePath={frontmatter.competition.table.file.path} />",
);
expect(competition.table.file.path).toBe(
const competition = filesFor(readPage("competition"), "CompetitionBase");
expect(competition.table).toBe(
"website/public/schemas/yaml/versions/v0.5.0/CompetitionBase.yaml",
);
const table = load(competition.table.file.path);
const table = load(competition.table);
expect(table.required).toContain("opportunity");
expect(table.required).not.toContain("opportunityId");
});

it("keeps the JSON Schema tab on the unversioned authoring schema", () => {
expect(form.jsonSchema.file.path).toBe(
expect(formFiles.jsonSchema).toBe(
"website/public/schemas/yaml/FormBase.yaml",
);
expect(form.jsonSchema.file.path).not.toBe(form.table.file.path);
expect(load(form.jsonSchema.file.path).required).toEqual(
expect(formFiles.jsonSchema).not.toBe(formFiles.table);
expect(load(formFiles.jsonSchema).required).toEqual(
expect.arrayContaining(["name", "title"]),
);
});

/** The served file a model page tells readers to validate payloads with. */
const validationTarget = (model: string) => {
const text = readFileSync(
path.join(
REPO_ROOT,
`website/src/content/docs/protocol/models/${model}.mdx`,
),
"utf-8",
);
const text = readPage(model);
const target = text.match(
/To validate a payload, use [^`]*`(\/[^`]+)`/,
)?.[1];
Expand All @@ -401,7 +408,7 @@ describe("the Form and Competition docs pages", () => {

it("links and recommends only files a normal resolver can follow", async () => {
for (const download of [
form.jsonSchema.file.path,
formFiles.jsonSchema,
validationTarget("form"),
validationTarget("competition"),
]) {
Expand Down
122 changes: 122 additions & 0 deletions website/__tests__/lib/schema/schema-doc-mapper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "fs";
import { tmpdir } from "os";
import * as path from "path";
import {
buildSchemaDocMap,
SchemaDocMapper,
} from "@/lib/schema/schema-doc-mapper";

describe("buildSchemaDocMap", () => {
let contentDir: string;

/** Writes a page under the temporary docs directory. */
function writePage(relativePath: string, content: string): void {
const file = path.join(contentDir, relativePath);
mkdirSync(path.dirname(file), { recursive: true });
writeFileSync(file, content);
}

beforeAll(() => {
contentDir = mkdtempSync(path.join(tmpdir(), "schema-doc-map-"));

writePage(
"protocol/models/award.mdx",
[
"---",
"title: Award",
"---",
"",
"## AwardBase",
"",
'<SchemaTable schema="AwardBase" />',
"",
"<SchemaFormatTabs",
' schema="AwardBase"',
" typeSpec",
"/>",
"",
"## AwdIds",
"",
'<SchemaFormatTabs schema="AwdIds" typeSpec />',
"",
"## A template, which has no JSON Schema",
"",
'<SchemaFormatTabs typeSpec="ExtensibleEnumT" />',
].join("\n"),
);

writePage(
"protocol/types/string.mdx",
[
"---",
"title: String types",
"url:",
" jsonSchema:",
" code: |",
" $id: url.yaml",
" type: string",
"---",
"",
"<SchemaFormatTabs jsonSchema={frontmatter.url.jsonSchema} />",
"",
'<SchemaFormatTabs schema="email" typeSpec />',
].join("\n"),
);

// Sorts before types/, so types/string.mdx wins for `email`.
writePage(
"protocol/fields/email.mdx",
'<SchemaFormatTabs schema="email" typeSpec="Email" />\n',
);
});

afterAll(() => {
rmSync(contentDir, { recursive: true, force: true });
});

it("maps each schema a page names to that page", () => {
const map = buildSchemaDocMap(
path.join(contentDir, "protocol"),
contentDir,
);
expect(map.get("AwardBase")).toBe("/protocol/models/award");
expect(map.get("AwdIds")).toBe("/protocol/models/award");
expect(map.get("ExtensibleEnumT")).toBeUndefined();
});

it("maps an inline schema's JSON type to its page as well", () => {
const map = buildSchemaDocMap(
path.join(contentDir, "protocol"),
contentDir,
);
expect(map.get("url")).toBe("/protocol/types/string");
expect(map.get("string")).toBe("/protocol/types/string");
});

it("lets the later page win when two pages name the same schema", () => {
const map = buildSchemaDocMap(
path.join(contentDir, "protocol"),
contentDir,
);
expect(map.get("email")).toBe("/protocol/types/string");
});
});

describe("SchemaDocMapper", () => {
it("builds the map from the real protocol pages", () => {
SchemaDocMapper.clearCache();
expect(SchemaDocMapper.getSchemaDocPath("OrgRef")).toBe(
"/protocol/models/organization",
);
expect(SchemaDocMapper.getSchemaDocPath("Money")).toBe(
"/protocol/fields/money",
);
expect(SchemaDocMapper.getSchemaDocPath("PaginatedResultsInfo")).toBe(
"/protocol/pagination",
);
expect(SchemaDocMapper.getSchemaDocPath("string")).toBe(
"/protocol/types/string",
);
});
});
26 changes: 26 additions & 0 deletions website/__tests__/lib/schema/schema-files.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, it, expect } from "vitest";
import { schemaFilePath } from "@/lib/schema/schema-files";

// Reads the generated schemas — run `pnpm --filter website run build` first.
describe("schemaFilePath", () => {
it("gives the generated JSON Schema file for a schema", () => {
expect(schemaFilePath("AwardBase")).toBe(
"website/public/schemas/yaml/AwardBase.yaml",
);
});

it("gives a version's copy when asked for one", () => {
expect(schemaFilePath("CompetitionBase", "v0.5.0")).toBe(
"website/public/schemas/yaml/versions/v0.5.0/CompetitionBase.yaml",
);
});

it("fails for a schema that has no JSON Schema file", () => {
expect(() => schemaFilePath("NoSuchSchema")).toThrow(
/No JSON Schema for "NoSuchSchema"/,
);
expect(() => schemaFilePath("AwardBase", "v0.1.0")).toThrow(
/No JSON Schema for "AwardBase" in v0.1.0/,
);
});
});
Loading