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
5 changes: 5 additions & 0 deletions .bumpy/bytes-mock-bounds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@saeris/valicon": patch
---

Keep `bytes` mocks within Valimock's string bound. A Lexicon `maxLength` such as the 2 MB limit on `com.atproto.sync.subscribeRepos#commit` no longer produces megabytes of base64 that Valimock rejects and regenerates until its retry budget runs out, which made mocking that definition take over a second. A `minLength` larger than the bound now widens it instead of being reported as unsatisfiable.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ jobs:
cache: true

- run: vp check
- run: vp test
- run: vp test --coverage
- run: vp pack
153 changes: 153 additions & 0 deletions src/__tests__/fixtures/synthetic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import type { LexiconDoc } from "../../types/index.js";

/**
* Hand-written documents that use the Lexicon features the published
* fixtures happen not to: string and integer enums and constants, byte and
* grapheme minimums, nullable required fields, open unions without variants,
* top-level primitive definitions, and XRPC endpoints with every optional
* part left out or filled in.
*/
export const kitchenSink: LexiconDoc = {
lexicon: 1,
id: "com.example.kitchenSink",
description: "Every Lexicon feature the published fixtures do not use.",
defs: {
main: {
type: "record",
key: "any",
record: {
type: "object",
required: ["status", "when"],
nullable: ["note", "when"],
properties: {
status: { type: "string", enum: ["draft", "published"] },
note: { type: "string" },
kind: { type: "string", const: "note" },
title: {
type: "string",
minLength: 1,
maxLength: 100,
minGraphemes: 1,
maxGraphemes: 50
},
"content-type": { type: "string" },
priority: { type: "integer", enum: [1, 2, 3] },
version: { type: "integer", const: 1 },
when: { type: "string", format: "datetime" },
cover: { type: "blob" },
payload: { type: "bytes" },
embed: { type: "union", refs: [] },
tags: { type: "array", items: { type: "string" }, minLength: 1 }
}
}
},
history: {
type: "array",
items: { type: "ref", ref: "#entry" },
maxLength: 10
},
entry: {
type: "object",
properties: { at: { type: "string", format: "datetime" } }
},
photo: { type: "blob", accept: ["image/*"], maxSize: 1000 },
enabled: { type: "boolean" },
signature: { type: "bytes", minLength: 1, maxLength: 64 },
link: { type: "cid-link" },
count: { type: "integer", minimum: 0, maximum: 10 },
label: { type: "string", description: "A free-form label." },
anything: { type: "unknown" }
}
};

export const ping: LexiconDoc = {
lexicon: 1,
id: "com.example.ping",
defs: {
main: { type: "procedure", description: "Checks that the service is up." }
}
};

export const count: LexiconDoc = {
lexicon: 1,
id: "com.example.count",
defs: {
main: {
type: "query",
parameters: {
type: "params",
required: ["q"],
properties: {
q: { type: "string" },
limit: { type: "integer", default: 10 },
tags: { type: "array", items: { type: "string" } },
verbose: { type: "boolean", default: false }
}
},
errors: [{ name: "TooMany" }]
}
}
};

export const upload: LexiconDoc = {
lexicon: 1,
id: "com.example.upload",
defs: {
main: {
type: "procedure",
input: { encoding: "*/*", description: "The raw file." },
output: {
encoding: "application/json",
description: "The stored file.",
schema: { type: "ref", ref: "#result" }
}
},
result: {
type: "object",
required: ["cid"],
properties: { cid: { type: "string", format: "cid" } }
}
}
};

export const events: LexiconDoc = {
lexicon: 1,
id: "com.example.events",
defs: {
main: {
type: "subscription",
parameters: {
type: "params",
properties: { cursor: { type: "integer" } }
},
errors: [{ name: "FutureCursor", description: "The cursor is ahead." }]
}
}
};

export const feed: LexiconDoc = {
lexicon: 1,
id: "com.example.feed",
defs: {
main: {
type: "subscription",
message: {
description: "A feed event.",
schema: { type: "union", refs: ["#post"] }
}
},
post: {
type: "object",
properties: { text: { type: "string" } }
}
}
};

export const syntheticLexicons: LexiconDoc[] = [
kitchenSink,
ping,
count,
upload,
events,
feed
];
180 changes: 176 additions & 4 deletions src/__tests__/valimock.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import { describe, expect, it } from "vitest";
import { faker } from "@faker-js/faker";
import { describe, expect, it, vi } from "vitest";
import * as v from "valibot";
import { Valimock } from "valimock";
import {
atIdentifier,
atUri,
cid,
datetime,
did,
didPlc,
didWeb,
handle,
language,
nsid,
recordKey,
tid,
uri
} from "../actions/index.js";
import { readLexiconFixtures } from "./fixtures/lexicons.js";
import { resolvePlaceholder } from "./fixtures/samples.js";
import { compileLexicons } from "../methods/compileLexicon.js";
import { bytes } from "../schemas/bytes.js";
import {
CID_REGEX,
DATETIME_REGEX,
Expand All @@ -12,16 +29,24 @@ import {
NSID_REGEX,
TID_REGEX
} from "../regex.js";
import { lexiconMocks } from "../valimock.js";
import { union } from "../schemas/union.js";
import {
lexiconMocks,
mockCid,
mockHandle,
mockMimeType,
mockNsid
} from "../valimock.js";

describe("lexiconMocks", () => {
const compiled = compileLexicons(readLexiconFixtures(), {
resolve: resolvePlaceholder
});
const warnings: string[] = [];
const mock = new Valimock({
...lexiconMocks,
seed: 42,
onWarn: () => {}
onWarn: (warning) => warnings.push(warning)
}).mock;

const schemas = Object.entries(compiled).flatMap(([nsid, defs]) =>
Expand All @@ -32,13 +57,37 @@ describe("lexiconMocks", () => {
);

// Every schema compiled from a real Lexicon must produce mocks that pass
// its own validation, otherwise the mocks are useless as test data.
// its own validation, otherwise the mocks are useless as test data. A
// warning means Valimock exhausted its retry budget on some field, which
// is both a generator that disagrees with the schema and the slow path
// that made this suite time out.
it.each(schemas)("should mock valid data for %s", (_name, schema) => {
warnings.length = 0;
for (let round = 0; round < 3; round++) {
const value: unknown = mock(schema);
const result = v.safeParse(schema, value);
expect(result.issues ?? []).toStrictEqual([]);
}
expect(warnings).toStrictEqual([]);
});

it("should keep bytes mocks small and within Lexicon limits", () => {
// `com.atproto.sync.subscribeRepos#commit` allows a 2 MB CAR file. A mock
// that size is useless as test data and far exceeds Valimock's string
// bound, which it would then retry sixteen times per field.
warnings.length = 0;
const large = mock(bytes({ maxLength: 2_000_000 }));
expect(large.$bytes.length).toBeLessThanOrEqual(128);
// A minimum above the string bound must still be honored rather than
// reported as unsatisfiable.
const small = mock(bytes({ minLength: 200, maxLength: 300 }));
expect(small.$bytes.length).toBeGreaterThanOrEqual(
Math.ceil((200 * 4) / 3)
);
expect(
v.safeParse(bytes({ minLength: 200, maxLength: 300 }), small).success
).toBe(true);
expect(warnings).toStrictEqual([]);
});

it("should generate realistic identifiers", () => {
Expand Down Expand Up @@ -95,6 +144,129 @@ describe("lexiconMocks", () => {
expect(blob.size).toBeLessThanOrEqual(2_000_000);
});

it("should mock every format action", () => {
const formats: Record<string, () => v.GenericValidation<string>> = {
atIdentifier,
atUri,
cid,
datetime,
did,
didPlc,
didWeb,
handle,
language,
nsid,
recordKey,
tid,
uri
};
for (const [name, format] of Object.entries(formats)) {
const schema = v.pipe(v.string(), format());
// Every top-level mock call reseeds, so one call for a batch of
// values exercises the generator's own variety.
for (const value of mock(v.pipe(v.array(schema), v.length(20)))) {
expect(v.safeParse(schema, value).success, `${name}: ${value}`).toBe(
true
);
}
}
});

it("should pick MIME types that satisfy accept lists", () => {
const mimeType = /^[a-z]+\/\S+$/u;
expect(mockMimeType(faker, [])).toMatch(mimeType);
expect(mockMimeType(faker, ["*/*"])).toMatch(mimeType);
expect(mockMimeType(faker, ["image/*"])).toMatch(/^image\//u);
// Groups Valicon has no examples for still get a type of the right kind.
expect(mockMimeType(faker, ["font/*"])).toMatch(/^font\/\S+$/u);
expect(mockMimeType(faker, ["application/json"])).toBe("application/json");
});

it("should generate CIDs for both atproto codecs", () => {
// Records and commits are dag-cbor, blobs are raw bytes, and the codec
// is visible in the CID prefix.
expect(mockCid(faker)).toMatch(/^bafyrei/u);
expect(mockCid(faker, "raw")).toMatch(/^bafkrei/u);
expect(mockCid(faker, "raw")).toMatch(CID_REGEX);
});

it("should never produce an empty handle label or NSID segment", () => {
// Faker words can consist entirely of characters a handle label or an
// NSID segment cannot contain, so the generators must substitute a
// placeholder rather than emit an invalid identifier.
const username = vi
.spyOn(faker.internet, "username")
.mockReturnValue("___");
const noun = vi.spyOn(faker.word, "noun").mockReturnValue("ÉTÉ");
try {
expect(mockHandle(faker)).toMatch(/^user\./u);
expect(mockHandle(faker)).toMatch(HANDLE_REGEX);
expect(mockNsid(faker)).toContain(".feed.");
expect(mockNsid(faker)).toMatch(NSID_REGEX);
} finally {
username.mockRestore();
noun.mockRestore();
}
});

it("should tolerate hand-built actions without a requirement", () => {
// Valimock dispatches on the action type alone, so a schema built by
// another tool may carry a Valicon action type without its requirement.
const bare = (type: string) =>
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
({
kind: "validation",
type,
async: false
}) as v.GenericValidation<string>;
for (const type of ["accept", "min_decoded_bytes", "max_decoded_bytes"]) {
const value = mock(v.pipe(v.string(), bare(type)));
expect(value.length).toBeGreaterThan(0);
}
});

it("should require Valimock 1.7 so the lazy mock can recurse", () => {
const lazy = lexiconMocks.customMocks?.lazy;
if (!lazy) {
throw new Error("expected a lazy mock");
}
expect(() => lazy(v.lazy(() => v.string()))).toThrow(
"Valimock 1.7 or newer"
);
});

it("should fail loudly for lazy schemas that resolve to nothing", () => {
const lazy = lexiconMocks.customMocks?.lazy;
if (!lazy) {
throw new Error("expected a lazy mock");
}
const instance = new Valimock({ ...lexiconMocks, seed: 42 });
expect(() => lazy(v.string(), instance.options, instance.mock)).toThrow(
"Unable to resolve lazy schema"
);
expect(() =>
lazy(
v.lazy(() => null as never),
instance.options,
instance.mock
)
).toThrow("Unable to resolve lazy schema");
});

it("should stamp $type on whatever a variant mock returns", () => {
const lazy = lexiconMocks.customMocks?.lazy;
if (!lazy) {
throw new Error("expected a lazy mock");
}
const instance = new Valimock({ ...lexiconMocks, seed: 42 });
const embed = union([
v.looseObject({ $type: v.literal("com.example.embed") })
]);
expect(lazy(embed, instance.options, () => "not an object")).toStrictEqual({
$type: "com.example.embed"
});
});

it("should leave plain strings to the default generator", () => {
const value = mock(v.object({ firstName: v.string() }));
expect(typeof value.firstName).toBe("string");
Expand Down
Loading
Loading