test(environment): consolidate form plan and serving checks (#11982)

This commit is contained in:
morluto authored and GitHub committed 2026-11-18 12:49:23 +08:00
1 parent ddb711270d
commit d09590dcdb
2 files changed
+51 -83

No files matched your search

@@ -21,6 +21,8 @@ const field = (body: JsonValue | undefined, name: string): JsonValue[] => {
return body.map((item) => (isRecord(item) ? (item[name] ?? null) : null));
};
const BY_NAME = { rule: "sorted", field: "name", direction: "ascending" };
describe("served creates and updates modelled on form recordings", () => {
it("answers a create with the nested members its recorded answer showed, storing those its read showed", () => {
const tasks = servedFrom(PARTIAL_OWNER_ROWS);
@@ -38,6 +40,18 @@ describe("served creates and updates modelled on form recordings", () => {
it("creates an entity from its name alone, storing the members the form sent empty as it sent them", () => {
const labels = servedFrom(formLabelRows());
expect(labels.state.writes.get("post_labels")).toMatchObject({
served: true,
body: {
accepted: ["name", "description", "color", "icon"],
required: ["name"],
blank: [
{ member: "color", sent: "" },
{ member: "icon", sent: "" },
{ member: "description", sent: "" },
],
},
});
expect(
labels.answer("post_labels", { body: { name: "Loaned Out" } }).body,
).toEqual({
@@ -70,9 +84,15 @@ describe("served creates and updates modelled on form recordings", () => {
expect(tags([])).toEqual([]);
expect(tags(null)).toBeNull();
});
});
describe("served collection changes and reads", () => {
it("moves a renamed entity to where the collection's order puts its new name", () => {
const labels = servedFrom(RENAMED_LABEL_ROWS);
expect(labels.state.writes.get("patch_labels_by_id")).toMatchObject({
served: true,
insert: BY_NAME,
});
labels.answer("patch_labels_by_id", { id: "1", body: { name: "Zinc" } });
expect(field(labels.answer("get_labels").body, "name")).toEqual([
"Tools",
@@ -82,6 +102,13 @@ describe("served creates and updates modelled on form recordings", () => {
it("keeps folders by title after a served create and rename", () => {
const folders = servedFrom(TITLE_ORDER_ROWS);
const byTitle = { rule: "sorted", field: "title", direction: "ascending" };
for (const tool of ["post_folders", "put_folders_by_id"]) {
expect(folders.state.writes.get(tool)).toMatchObject({
served: true,
insert: byTitle,
});
}
folders.answer("post_folders", { body: { title: "Harbour Ops" } });
expect(field(folders.answer("get_folders").body, "title")).toEqual([
"Harbour Ops",
@@ -129,6 +156,14 @@ describe("served creates and updates modelled on form recordings", () => {
it("accepts a member sent empty as one recording did, and stores the default for one left out", () => {
const notes = servedFrom(NOTE_DEFAULT_ROWS);
expect(notes.state.writes.get("post_notes")).toMatchObject({
served: true,
body: {
required: ["name"],
defaults: [{ member: "note", value: "" }],
blank: [{ member: "note", sent: null }],
},
});
expect(
notes.answer("post_notes", { body: { name: "Blank", note: null } }).body,
).toMatchObject({ name: "Blank", note: null });
@@ -139,6 +174,14 @@ describe("served creates and updates modelled on form recordings", () => {
it("answers writes with empty lists spelled as their recorded answers spelled them", () => {
const tasks = servedFrom(UNLOADED_ANSWER_ROWS);
expect(tasks.state.writes.get("post_tasks")).toMatchObject({
served: true,
observation: { read: { shape: "item" } },
});
expect(tasks.state.writes.get("patch_tasks_by_id")).toMatchObject({
served: true,
rules: [{ field: "done", rule: "echo" }],
});
expect(
tasks.answer("post_tasks", { body: { title: "Coil the ropes" } }).body,
).toMatchObject({
@@ -173,6 +216,14 @@ describe("served creates and updates modelled on form recordings", () => {
it("lists a created tag's id in the list envelope's ids, in list order", () => {
const tags = servedFrom(LISTED_ID_ROWS);
expect(tags.state.views.get("get_tags")).toMatchObject({
shape: "list",
listed_ids: [{ member: "all", field: "id" }],
});
expect(tags.state.writes.get("post_tags")).toMatchObject({
served: true,
insert: BY_NAME,
});
tags.answer("post_tags", { body: { name: "Lamp" } });
expect(tags.answer("get_tags").body).toMatchObject({
count: 3,
@@ -8,12 +8,8 @@ import {
BULK_EMPTY_TAG_ROWS,
CREATION_ORDER_ROWS,
DERIVED_TEXT_ROWS,
formLabelRows,
ID_OR_TITLE_ROWS,
LISTED_ID_ROWS,
MINIMAL_CONTACT_ROWS,
NOTE_DEFAULT_ROWS,
RENAMED_LABEL_ROWS,
SHELF_SORTED_ROWS,
SHELF_TIE_ROWS,
SHELF_GUESS_ROWS,
@@ -21,8 +17,6 @@ import {
UNSORTED_PENDING_ROWS,
IN_PLACE_THEN_LAST_ROWS,
IN_PLACE_THEN_SORTED_ROWS,
TITLE_ORDER_ROWS,
UNLOADED_ANSWER_ROWS,
UNSORTED_SEED_ROWS,
CONFIRMED_RENAME_ROWS,
UNPLACED_CREATE_ROWS,
@@ -47,21 +41,6 @@ const BY_NAME = {
} as const;
describe("create members a form sent empty", () => {
it("requires only the members some recorded create filled in, and applies the others as they were sent", () => {
expect(planOf(formLabelRows(), "post_labels")).toMatchObject({
served: true,
body: {
accepted: ["name", "description", "color", "icon"],
required: ["name"],
blank: [
{ member: "color", sent: "" },
{ member: "icon", sent: "" },
{ member: "description", sent: "" },
],
},
});
});
it("accepts either spelling of a member two creates sent empty differently, filling the earliest", () => {
const body: WriteBody = {
accepted: ["name", "note"],
@@ -96,19 +75,6 @@ describe("create members a form sent empty", () => {
});
});
describe("empty values and defaults", () => {
it("keeps accepting a recorded empty value after a later call that left the member out teaches its default", () => {
expect(planOf(NOTE_DEFAULT_ROWS, "post_notes")).toMatchObject({
served: true,
body: {
required: ["name"],
defaults: [{ member: "note", value: "" }],
blank: [{ member: "note", sent: null }],
},
});
});
});
describe("a create with only what a person must give", () => {
it("serves a create whose minimal call left string members `null` and an empty flag spelled another way", () => {
expect(planOf(MINIMAL_CONTACT_ROWS, "post_contacts")).toMatchObject({
@@ -127,25 +93,6 @@ describe("a create with only what a person must give", () => {
});
describe("collections kept sorted", () => {
it("moves an updated entity by the field its collection's creates insert by", () => {
expect(planOf(RENAMED_LABEL_ROWS, "patch_labels_by_id")).toMatchObject({
served: true,
insert: BY_NAME,
});
});
it("learns the sort from a rename that moves a folder, and inserts creates by it", () => {
const byTitle = { rule: "sorted", field: "title", direction: "ascending" };
expect(planOf(TITLE_ORDER_ROWS, "put_folders_by_id")).toMatchObject({
served: true,
insert: byTitle,
});
expect(planOf(TITLE_ORDER_ROWS, "post_folders")).toMatchObject({
served: true,
insert: byTitle,
});
});
it("learns the title order from a rename when the creates fit both id and title order", () => {
const byTitle = { rule: "sorted", field: "title", direction: "ascending" };
expect(planOf(ID_OR_TITLE_ROWS, "put_folders_by_id")).toMatchObject({
@@ -250,39 +197,9 @@ describe("collections kept sorted", () => {
);
expect(order).toEqual([a, d, movedC, movedB]);
});
it("renders a list envelope's ids of the listed entities from the state", () => {
const { views, writes } = environmentStateViewOf(
timedCaptureEvidence(LISTED_ID_ROWS),
);
const list = views.find(({ tool }) => tool === "get_tags");
const create = writes.find(({ tool }) => tool === "post_tags");
expect(list).toMatchObject({
shape: "list",
listed_ids: [{ member: "all", field: "id" }],
});
expect(create).toMatchObject({
served: true,
insert: BY_NAME,
});
});
});
describe("answers that show stored values otherwise than reads", () => {
it("takes the later read's values where a create's answer had not loaded them", () => {
expect(planOf(UNLOADED_ANSWER_ROWS, "post_tasks")).toMatchObject({
served: true,
observation: { read: { shape: "item" } },
});
});
it("reads an empty list spelled `[]` in an update's answer and `null` in reads as unchanged", () => {
expect(planOf(UNLOADED_ANSWER_ROWS, "patch_tasks_by_id")).toMatchObject({
served: true,
rules: [{ field: "done", rule: "echo" }],
});
});
it("withholds a later create that stored an unruled field otherwise, without blocking checks of later writes", () => {
expect(planOf(DERIVED_TEXT_ROWS, "post_tags")).toMatchObject({
served: false,