feat(profile): add the manual candidate profile contract and storage
This commit is contained in:
1 parent
d21fd49a42
commit
e52ffe7bcb
97 files changed
+20442
-63
No files matched your search
@@ -138,17 +138,34 @@ describe("rewriteMockSource", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("fails when the schemas disagree on the bounds", () => {
|
||||
const ambiguous = buildPatternBoundsIndex({
|
||||
it("uses the common bounds when schemas share a property name and a pattern", () => {
|
||||
const shared = buildPatternBoundsIndex({
|
||||
components: {
|
||||
schemas: {
|
||||
A: { properties: { id: { type: "string", maxLength: 8, pattern: "^a+$" } } },
|
||||
A: {
|
||||
properties: { id: { type: "string", minLength: 1, maxLength: 8, pattern: "^a+$" } },
|
||||
},
|
||||
B: { properties: { id: { type: "string", maxLength: 9, pattern: "^a+$" } } },
|
||||
},
|
||||
},
|
||||
});
|
||||
const source = `${header}\nexport const m = () => ({id: faker.helpers.fromRegExp("^a+$")})\n`;
|
||||
expect(() => rewrite(source, ambiguous)).toThrow("so the mock bounds are ambiguous");
|
||||
expect(rewrite(source, shared).text).toContain(
|
||||
'fakeFromPattern("^a+$", {minLength: 1, maxLength: 8})',
|
||||
);
|
||||
});
|
||||
|
||||
it("fails when the bounds of schemas have no common value", () => {
|
||||
const disjoint = buildPatternBoundsIndex({
|
||||
components: {
|
||||
schemas: {
|
||||
A: { properties: { id: { type: "string", maxLength: 3, pattern: "^a+$" } } },
|
||||
B: { properties: { id: { type: "string", minLength: 5, pattern: "^a+$" } } },
|
||||
},
|
||||
},
|
||||
});
|
||||
const source = `${header}\nexport const m = () => ({id: faker.helpers.fromRegExp("^a+$")})\n`;
|
||||
expect(() => rewrite(source, disjoint)).toThrow("so the mock bounds are ambiguous");
|
||||
});
|
||||
|
||||
it("fails on a call that it cannot attribute to a property", () => {
|
||||
|
||||
@@ -13,7 +13,8 @@
|
||||
// src/shared/api/mock-pattern.ts. The bounds come from the OpenAPI document:
|
||||
// the call's enclosing object property and the pattern identify the schema.
|
||||
// The rewrite fails when a call has an unexpected form, when no schema
|
||||
// matches, or when schemas that match disagree on the bounds, so a contract
|
||||
// matches, or when the bounds of schemas that match have no common value;
|
||||
// schemas that match with different bounds share their intersection, so a contract
|
||||
// change cannot silently produce invalid mocks. Running it again on its own
|
||||
// output changes nothing.
|
||||
//
|
||||
@@ -207,14 +208,29 @@ function findBounds(
|
||||
`${location}: no schema property "${property}" with pattern ${JSON.stringify(pattern)} in the OpenAPI document.`,
|
||||
);
|
||||
}
|
||||
if (candidates.length > 1) {
|
||||
if (candidates.length === 1) {
|
||||
return bounds;
|
||||
}
|
||||
// The generated mock does not say which schema owns the call, so schemas
|
||||
// that share a property name and a pattern share one set of bounds: the
|
||||
// intersection. A value inside it satisfies every one of those schemas.
|
||||
const minLength = Math.max(...candidates.map((item) => item.minLength ?? 0));
|
||||
const maxima = candidates.flatMap((item) =>
|
||||
item.maxLength === undefined ? [] : [item.maxLength],
|
||||
);
|
||||
const maxLength = maxima.length === 0 ? undefined : Math.min(...maxima);
|
||||
if (maxLength !== undefined && minLength > maxLength) {
|
||||
throw new Error(
|
||||
`${location}: properties named "${property}" with pattern ${JSON.stringify(pattern)} have different length bounds (${candidates
|
||||
`${location}: properties named "${property}" with pattern ${JSON.stringify(pattern)} have length bounds (${candidates
|
||||
.map(describeBounds)
|
||||
.join("; ")}), so the mock bounds are ambiguous.`,
|
||||
.join("; ")}) that no value satisfies together, so the mock bounds are ambiguous.`,
|
||||
);
|
||||
}
|
||||
return bounds;
|
||||
const anyMin = candidates.some((item) => item.minLength !== undefined);
|
||||
return {
|
||||
...(anyMin ? { minLength } : {}),
|
||||
...(maxLength === undefined ? {} : { maxLength }),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user