@sdxc/kit 2026.10.17 → 2026.10.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +397 -22
- package/changelog.jsonl +1171 -1092
- package/config.schema.json +53 -1
- package/dist/cli.js +12 -0
- package/dist/commands/deploy.d.ts +23 -0
- package/dist/commands/deploy.js +167 -0
- package/dist/commands/destroy.d.ts +22 -0
- package/dist/commands/destroy.js +92 -0
- package/dist/commands/doctor.d.ts +26 -0
- package/dist/commands/doctor.js +65 -0
- package/dist/commands/initializers.d.ts +16 -0
- package/dist/commands/initializers.js +23 -0
- package/dist/commands/make.d.ts +78 -6
- package/dist/commands/make.js +134 -20
- package/dist/commands/middleware.d.ts +16 -0
- package/dist/commands/middleware.js +21 -0
- package/dist/commands/models.d.ts +18 -0
- package/dist/commands/models.js +29 -0
- package/dist/commands/notes.d.ts +17 -0
- package/dist/commands/notes.js +27 -0
- package/dist/commands/routes.d.ts +25 -0
- package/dist/commands/routes.js +50 -0
- package/dist/commands/run.d.ts +16 -0
- package/dist/commands/run.js +32 -11
- package/dist/commands/scaffold.d.ts +30 -0
- package/dist/commands/scaffold.js +34 -0
- package/dist/commands/schedule.d.ts +16 -0
- package/dist/commands/schedule.js +21 -0
- package/dist/commands/stats.d.ts +16 -0
- package/dist/commands/stats.js +21 -0
- package/dist/commands/ui/update.d.ts +6 -3
- package/dist/commands/ui/update.js +24 -7
- package/dist/commands/upgrade.d.ts +3 -1
- package/dist/commands/upgrade.js +88 -23
- package/dist/config.d.ts +20 -0
- package/dist/config.js +32 -1
- package/dist/contract.d.ts +101 -3
- package/dist/contract.js +371 -26
- package/dist/create/templates.js +3 -2
- package/dist/dependencies.d.ts +5 -1
- package/dist/dependencies.js +16 -1
- package/dist/deploy/plan.d.ts +49 -0
- package/dist/deploy/plan.js +68 -0
- package/dist/destroy.d.ts +40 -0
- package/dist/destroy.js +317 -0
- package/dist/doctor/check.d.ts +71 -0
- package/dist/doctor/check.js +53 -0
- package/dist/doctor/checks.d.ts +23 -0
- package/dist/doctor/checks.js +70 -0
- package/dist/doctor/define.d.ts +59 -0
- package/dist/doctor/define.js +16 -0
- package/dist/doctor/emails.d.ts +18 -0
- package/dist/doctor/emails.js +243 -0
- package/dist/doctor/env.d.ts +26 -0
- package/dist/doctor/env.js +88 -0
- package/dist/doctor/files.d.ts +14 -0
- package/dist/doctor/files.js +34 -0
- package/dist/doctor/fixture.d.ts +23 -0
- package/dist/doctor/fixture.js +46 -0
- package/dist/doctor/install.d.ts +17 -0
- package/dist/doctor/install.js +37 -0
- package/dist/doctor/installed.d.ts +11 -0
- package/dist/doctor/installed.js +79 -0
- package/dist/doctor/jsdoc.d.ts +44 -0
- package/dist/doctor/jsdoc.js +201 -0
- package/dist/doctor/layout.d.ts +23 -0
- package/dist/doctor/layout.js +83 -0
- package/dist/doctor/migrations.d.ts +16 -0
- package/dist/doctor/migrations.js +96 -0
- package/dist/doctor/models.d.ts +13 -0
- package/dist/doctor/models.js +69 -0
- package/dist/doctor/pins.d.ts +15 -0
- package/dist/doctor/pins.js +79 -0
- package/dist/doctor/project.d.ts +58 -0
- package/dist/doctor/project.js +261 -0
- package/dist/doctor/remix.d.ts +14 -0
- package/dist/doctor/remix.js +44 -0
- package/dist/doctor/routes.d.ts +11 -0
- package/dist/doctor/routes.js +49 -0
- package/dist/doctor/typegen.d.ts +13 -0
- package/dist/doctor/typegen.js +57 -0
- package/dist/doctor/ui.d.ts +14 -0
- package/dist/doctor/ui.js +51 -0
- package/dist/initializers.d.ts +25 -0
- package/dist/initializers.js +89 -0
- package/dist/lock.d.ts +2 -2
- package/dist/make/actions.d.ts +23 -0
- package/dist/make/actions.js +82 -0
- package/dist/make/declare.d.ts +31 -3
- package/dist/make/declare.js +284 -9
- package/dist/make/email.d.ts +23 -0
- package/dist/make/email.js +111 -0
- package/dist/make/from-fields.d.ts +30 -0
- package/dist/make/from-fields.js +162 -0
- package/dist/make/from-model.d.ts +39 -0
- package/dist/make/from-model.js +161 -0
- package/dist/make/generate.d.ts +25 -5
- package/dist/make/generate.js +76 -22
- package/dist/make/guard.d.ts +35 -0
- package/dist/make/guard.js +73 -0
- package/dist/make/imports.d.ts +6 -0
- package/dist/make/imports.js +19 -0
- package/dist/make/index.d.ts +49 -4
- package/dist/make/index.js +290 -29
- package/dist/make/kinds.d.ts +34 -2
- package/dist/make/kinds.js +92 -8
- package/dist/make/mount.d.ts +25 -0
- package/dist/make/mount.js +135 -0
- package/dist/make/schema-diff.d.ts +58 -0
- package/dist/make/schema-diff.js +609 -0
- package/dist/make/schema.d.ts +76 -0
- package/dist/make/schema.js +206 -0
- package/dist/make/stubs.d.ts +14 -7
- package/dist/make/stubs.js +24 -8
- package/dist/make/test.d.ts +43 -0
- package/dist/make/test.js +150 -0
- package/dist/make/trigger.d.ts +31 -0
- package/dist/make/trigger.js +163 -0
- package/dist/models.d.ts +42 -0
- package/dist/models.js +149 -0
- package/dist/notes.d.ts +28 -0
- package/dist/notes.js +66 -0
- package/dist/reference.d.ts +27 -0
- package/dist/reference.js +103 -0
- package/dist/releases.d.ts +9 -0
- package/dist/releases.js +29 -8
- package/dist/routes/chains.d.ts +34 -0
- package/dist/routes/chains.js +95 -0
- package/dist/routes/filter.d.ts +53 -0
- package/dist/routes/filter.js +147 -0
- package/dist/routes/format.d.ts +23 -0
- package/dist/routes/format.js +123 -0
- package/dist/routes/helpers.d.ts +21 -0
- package/dist/routes/helpers.js +165 -0
- package/dist/routes/index.d.ts +56 -0
- package/dist/routes/index.js +167 -0
- package/dist/routes/modules.d.ts +116 -0
- package/dist/routes/modules.js +235 -0
- package/dist/routes/parse.d.ts +161 -0
- package/dist/routes/parse.js +621 -0
- package/dist/routes/routers.d.ts +69 -0
- package/dist/routes/routers.js +327 -0
- package/dist/scaffold/fields.d.ts +129 -0
- package/dist/scaffold/fields.js +421 -0
- package/dist/scaffold/index.d.ts +39 -0
- package/dist/scaffold/index.js +622 -0
- package/dist/scaffold/render.d.ts +61 -0
- package/dist/scaffold/render.js +417 -0
- package/dist/schedule.d.ts +45 -0
- package/dist/schedule.js +124 -0
- package/dist/stats.d.ts +37 -0
- package/dist/stats.js +126 -0
- package/dist/ui/commands.d.ts +10 -3
- package/dist/ui/commands.js +85 -15
- package/dist/ui/registry.d.ts +8 -8
- package/dist/upgrade/agents.d.ts +49 -0
- package/dist/upgrade/agents.js +110 -0
- package/dist/upgrade/changelog.d.ts +2 -2
- package/dist/upgrade/project.d.ts +4 -0
- package/dist/upgrade/project.js +11 -1
- package/dist/upgrade/prompt.d.ts +11 -0
- package/dist/upgrade/prompt.js +50 -7
- package/package.json +6 -4
- package/registry/ui/alert-dialog.json +2 -2
- package/registry/ui/attachment.json +2 -2
- package/registry/ui/calendar.json +2 -2
- package/registry/ui/color-picker.json +2 -2
- package/registry/ui/command.json +2 -2
- package/registry/ui/date-picker.json +2 -2
- package/registry/ui/date-range-picker.json +2 -2
- package/registry/ui/dialog.json +2 -2
- package/registry/ui/file-trigger.json +2 -2
- package/registry/ui/heading-scope.json +2 -2
- package/registry/ui/message-scroller.json +2 -2
- package/registry/ui/number-field.json +2 -2
- package/registry/ui/range-calendar.json +2 -2
- package/registry/ui/resizable.json +2 -2
- package/registry/ui/select.json +2 -2
- package/registry/ui/shared-element.json +2 -2
- package/registry/ui/sidebar.json +3 -3
- package/registry/ui/typeset.json +3 -3
- package/stubs/command.spec.stub +15 -0
- package/stubs/controller.api.resource.stub +23 -0
- package/stubs/controller.api.spec.stub +15 -0
- package/stubs/controller.form.stub +2 -2
- package/stubs/controller.form.test.stub +2 -2
- package/stubs/controller.resource.form.stub +67 -0
- package/stubs/controller.resource.stub +28 -0
- package/stubs/controller.spec.stub +15 -0
- package/stubs/email.stub +9 -14
- package/stubs/email.test.stub +9 -9
- package/stubs/factory.stub +1 -1
- package/stubs/mcp.spec.stub +16 -0
- package/stubs/model.stub +3 -3
- package/stubs/policy.test.stub +34 -8
- package/stubs/scaffold.api.create.stub +34 -0
- package/stubs/scaffold.api.create.test.stub +37 -0
- package/stubs/scaffold.api.destroy.stub +34 -0
- package/stubs/scaffold.api.destroy.test.stub +44 -0
- package/stubs/scaffold.api.index.stub +23 -0
- package/stubs/scaffold.api.index.test.stub +31 -0
- package/stubs/scaffold.api.show.stub +27 -0
- package/stubs/scaffold.api.show.test.stub +41 -0
- package/stubs/scaffold.api.update.stub +44 -0
- package/stubs/scaffold.api.update.test.stub +46 -0
- package/stubs/scaffold.command.create.stub +22 -0
- package/stubs/scaffold.command.create.test.stub +30 -0
- package/stubs/scaffold.command.delete.stub +20 -0
- package/stubs/scaffold.command.delete.test.stub +39 -0
- package/stubs/scaffold.command.list.stub +18 -0
- package/stubs/scaffold.command.list.test.stub +31 -0
- package/stubs/scaffold.command.show.stub +19 -0
- package/stubs/scaffold.command.show.test.stub +37 -0
- package/stubs/scaffold.controller.destroy.stub +35 -0
- package/stubs/scaffold.controller.destroy.test.stub +65 -0
- package/stubs/scaffold.controller.edit.stub +97 -0
- package/stubs/scaffold.controller.edit.test.stub +91 -0
- package/stubs/scaffold.controller.index.stub +55 -0
- package/stubs/scaffold.controller.index.test.stub +32 -0
- package/stubs/scaffold.controller.new.stub +70 -0
- package/stubs/scaffold.controller.new.test.stub +81 -0
- package/stubs/scaffold.controller.show.stub +59 -0
- package/stubs/scaffold.controller.show.test.stub +43 -0
- package/stubs/scaffold.factory.stub +16 -0
- package/stubs/scaffold.form.stub +75 -0
- package/stubs/scaffold.mcp.create.stub +24 -0
- package/stubs/scaffold.mcp.create.test.stub +36 -0
- package/stubs/scaffold.mcp.delete.stub +31 -0
- package/stubs/scaffold.mcp.delete.test.stub +43 -0
- package/stubs/scaffold.mcp.get.stub +21 -0
- package/stubs/scaffold.mcp.get.test.stub +31 -0
- package/stubs/scaffold.mcp.list.stub +23 -0
- package/stubs/scaffold.mcp.list.test.stub +25 -0
- package/stubs/scaffold.mcp.update.stub +37 -0
- package/stubs/scaffold.mcp.update.test.stub +40 -0
- package/stubs/scaffold.migration.stub +3 -0
- package/stubs/scaffold.model.stub +35 -0
- package/stubs/scaffold.model.test.stub +25 -0
- package/stubs/scaffold.spec.stub +7 -0
- package/stubs/scaffold.test-database.stub +33 -0
- package/stubs/scaffold.test-mcp.stub +92 -0
- package/stubs/scaffold.validator.stub +18 -0
- package/stubs/scaffold.validator.test.stub +26 -0
- package/templates/app/AGENTS.md +167 -0
- package/templates/app/CLAUDE.md +1 -0
- package/templates/app/README.md +33 -15
- package/templates/app/app/commands/console.test.ts +26 -0
- package/templates/app/app/commands/console.ts +19 -0
- package/templates/app/app/commands/db/fresh.test.ts +57 -0
- package/templates/app/app/commands/db/fresh.ts +37 -0
- package/templates/app/app/commands/db/migrate.test.ts +17 -1
- package/templates/app/app/commands/db/migrate.ts +51 -18
- package/templates/app/app/commands/db/prepare.test.ts +39 -0
- package/templates/app/app/commands/db/prepare.ts +35 -0
- package/templates/app/app/commands/db/seed.test.ts +38 -0
- package/templates/app/app/commands/db/seed.ts +26 -0
- package/templates/app/app/commands/deploy.test.ts +34 -0
- package/templates/app/app/commands/deploy.ts +18 -0
- package/templates/app/app/commands/emails/preview.test.tsx +63 -0
- package/templates/app/app/commands/emails/preview.ts +24 -0
- package/templates/app/app/commands/index.ts +57 -2
- package/templates/app/app/commands/typegen.test.ts +20 -1
- package/templates/app/app/commands/typegen.ts +16 -2
- package/templates/app/app/emails/index.ts +10 -0
- package/templates/app/app/http/middleware/access.test.ts +43 -0
- package/templates/app/app/http/middleware/access.ts +41 -0
- package/templates/app/app/http/middleware/api.ts +12 -3
- package/templates/app/app/http/middleware/page.test.ts +4 -1
- package/templates/app/app/http/middleware/page.ts +7 -5
- package/templates/app/app/initializers/cli.ts +35 -2
- package/templates/app/app/initializers/dispatcher.ts +9 -1
- package/templates/app/app/initializers/mail-transport.test.ts +33 -0
- package/templates/app/app/initializers/mail-transport.ts +26 -0
- package/templates/app/app/initializers/mail.test.ts +17 -17
- package/templates/app/app/initializers/mail.ts +15 -7
- package/templates/app/app/initializers/router.test.ts +7 -0
- package/templates/app/app/initializers/router.tsx +13 -3
- package/templates/app/app/jobs/email.test.tsx +70 -0
- package/templates/app/app/jobs/email.ts +16 -0
- package/templates/app/app/jobs/index.ts +6 -2
- package/templates/app/app/policies/abilities.ts +13 -0
- package/templates/app/app/policies/policy.test.ts +15 -0
- package/templates/app/app/policies/policy.ts +30 -0
- package/templates/app/app/services/commands.ts +30 -0
- package/templates/app/app/services/migrations.test.ts +48 -1
- package/templates/app/app/services/migrations.ts +66 -9
- package/templates/app/app/test/cloudflare-workers.ts +2 -0
- package/templates/app/app/test/local-database.ts +67 -0
- package/templates/app/app/test/session.ts +28 -0
- package/templates/app/bootstrap/console.ts +13 -0
- package/templates/app/cloudflare.config.ts +3 -3
- package/templates/app/database/seed.ts +12 -0
- package/templates/app/package.json +3 -1
- package/templates/app/spec/config.jsonc +16 -0
- package/stubs/policy.stub +0 -16
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The new {{words}} form through the app's router as a signed-in member, so the honeypot check
|
|
3
|
+
* and the access check run as in the Worker, and as a guest, whom `{{path}}.create` refuses. Its
|
|
4
|
+
* controller loads with the file, keeping its cold import outside each test's time limit.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/building-remix-apps/forms-and-params}
|
|
7
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { unwrap } from "@sdxc/result";
|
|
11
|
+
import { beforeAll, describe, expect, test } from "vitest";
|
|
12
|
+
|
|
13
|
+
import "~/app/http/controllers/{{plural}}/new";
|
|
14
|
+
import { openDatabase } from "~/app/initializers/database";
|
|
15
|
+
import { honeypot } from "~/app/initializers/honeypot";
|
|
16
|
+
import { createWebRouter } from "~/app/initializers/router";
|
|
17
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
18
|
+
import { migrate } from "~/app/test/database";
|
|
19
|
+
import { signIn } from "~/app/test/session";
|
|
20
|
+
import routes from "~/routes/web";
|
|
21
|
+
|
|
22
|
+
beforeAll(migrate);
|
|
23
|
+
|
|
24
|
+
/** Posts `fields` to the create action with valid honeypot fields, as `cookie`'s visitor. */
|
|
25
|
+
async function submit(fields: Record<string, string>, cookie: string | null) {
|
|
26
|
+
let { token, tokenField } = unwrap(await honeypot.issue());
|
|
27
|
+
return createWebRouter().fetch(
|
|
28
|
+
new Request(new URL(routes.{{pluralKey}}.new.action.href(), "https://example.com"), {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: cookie === null ? {} : { cookie },
|
|
31
|
+
body: new URLSearchParams({ ...fields, [tokenField]: token }),
|
|
32
|
+
}),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Fetches the empty form as `cookie`'s visitor. */
|
|
37
|
+
function form(cookie: string | null) {
|
|
38
|
+
return createWebRouter().fetch(
|
|
39
|
+
new Request(new URL(routes.{{pluralKey}}.new.index.href(), "https://example.com"), {
|
|
40
|
+
headers: cookie === null ? {} : { cookie },
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe("{{plural}}/new", () => {
|
|
46
|
+
test("renders the form", async () => {
|
|
47
|
+
let response = await form(await signIn());
|
|
48
|
+
|
|
49
|
+
expect(response.status).toBe(200);
|
|
50
|
+
expect(await response.text()).toContain('name="{{firstColumn}}"');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("creates a {{words}} and redirects to its page", async () => {
|
|
54
|
+
let response = await submit({ {{formSample}} }, await signIn());
|
|
55
|
+
|
|
56
|
+
let rows = await {{Name}}.bind({ db: openDatabase() }).query().all();
|
|
57
|
+
expect(response.status).toBe(303);
|
|
58
|
+
expect(rows.map((row) => routes.{{pluralKey}}.show.href({ id: row.id }))).toContain(
|
|
59
|
+
response.headers.get("location"),
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("renders the form again for a refused submission", async () => {
|
|
64
|
+
let response = await submit({ {{invalidForm}} }, await signIn());
|
|
65
|
+
|
|
66
|
+
expect(response.status).toBe(422);
|
|
67
|
+
expect(await response.text()).toContain('name="{{firstColumn}}"');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("answers a guest 403 for the form and the action, creating nothing", async () => {
|
|
71
|
+
let before = await {{Name}}.bind({ db: openDatabase() }).query().all();
|
|
72
|
+
|
|
73
|
+
let page = await form(null);
|
|
74
|
+
let response = await submit({ {{formSample}} }, null);
|
|
75
|
+
|
|
76
|
+
let after = await {{Name}}.bind({ db: openDatabase() }).query().all();
|
|
77
|
+
expect(page.status).toBe(403);
|
|
78
|
+
expect(response.status).toBe(403);
|
|
79
|
+
expect(after).toHaveLength(before.length);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One {{words}}'s page: every field, a link to edit it and a button that deletes it. An id no
|
|
3
|
+
* {{words}} has, and one `{{path}}.read` hides, is a 404.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/building-remix-apps/interface-with-remix-ui}{{header}}
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { HoneypotFields } from "@sdxc/honeypot/ui";
|
|
10
|
+
import { unwrap } from "@sdxc/result";
|
|
11
|
+
import { Button } from "@sdxc/ui/button";
|
|
12
|
+
import { LinkButton } from "@sdxc/ui/link-button";
|
|
13
|
+
import { createAction } from "remix/router";
|
|
14
|
+
|
|
15
|
+
import page from "~/app/http/middleware/page";
|
|
16
|
+
import abilities from "~/app/policies/abilities";
|
|
17
|
+
import DocumentLayout from "~/resources/layouts/document";
|
|
18
|
+
import routes from "~/routes/web";
|
|
19
|
+
|
|
20
|
+
export default createAction(routes.{{pluralKey}}.show, {
|
|
21
|
+
middleware: page,
|
|
22
|
+
/** Renders the {{words}} with the delete form's honeypot fields. */
|
|
23
|
+
async handler(ctx) {
|
|
24
|
+
let row = await ctx.models.{{key}}.find(ctx.params.id);
|
|
25
|
+
if (row === null || !ctx.access.can(abilities.{{path}}.read, { {{key}}: row })) {
|
|
26
|
+
let missing = ctx.intl.t("notFound.title");
|
|
27
|
+
return ctx.render(
|
|
28
|
+
<DocumentLayout assets={ctx.assets} intl={ctx.intl} url={ctx.url} title={missing}>
|
|
29
|
+
<h1>{missing}</h1>
|
|
30
|
+
</DocumentLayout>,
|
|
31
|
+
{ status: 404 },
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
let title = ctx.intl.t("{{pluralKey}}.show.title");
|
|
35
|
+
let fields = unwrap(await ctx.honeypot.issue());
|
|
36
|
+
return ctx.render(
|
|
37
|
+
<DocumentLayout assets={ctx.assets} intl={ctx.intl} url={ctx.url} title={title}>
|
|
38
|
+
<h1>{title}</h1>
|
|
39
|
+
<dl>
|
|
40
|
+
{{detailRows}}
|
|
41
|
+
</dl>
|
|
42
|
+
<p>
|
|
43
|
+
<LinkButton href={routes.{{pluralKey}}.edit.index.href({ id: row.id })}>
|
|
44
|
+
{ctx.intl.t("{{pluralKey}}.show.edit")}
|
|
45
|
+
</LinkButton>{" "}
|
|
46
|
+
<LinkButton href={routes.{{pluralKey}}.index.href()} variant="ghost">
|
|
47
|
+
{ctx.intl.t("{{pluralKey}}.show.back")}
|
|
48
|
+
</LinkButton>
|
|
49
|
+
</p>
|
|
50
|
+
<form method="post" action={routes.{{pluralKey}}.destroy.href({ id: row.id })}>
|
|
51
|
+
<HoneypotFields {...fields} />
|
|
52
|
+
<Button type="submit" color="danger">
|
|
53
|
+
{ctx.intl.t("{{pluralKey}}.show.delete")}
|
|
54
|
+
</Button>
|
|
55
|
+
</form>
|
|
56
|
+
</DocumentLayout>,
|
|
57
|
+
);
|
|
58
|
+
},
|
|
59
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One {{words}}'s page, rendered through the app's router. Its controller loads with the file, so
|
|
3
|
+
* the cold import of its views stays outside each test's time limit.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { beforeAll, describe, expect, test } from "vitest";
|
|
10
|
+
|
|
11
|
+
import "~/app/http/controllers/{{plural}}/show";
|
|
12
|
+
import { createWebRouter } from "~/app/initializers/router";
|
|
13
|
+
import { factories, migrate } from "~/app/test/database";
|
|
14
|
+
import { {{Name}}Factory } from "~/database/factories/{{name}}";
|
|
15
|
+
import routes from "~/routes/web";
|
|
16
|
+
|
|
17
|
+
beforeAll(migrate);
|
|
18
|
+
|
|
19
|
+
/** Fetches the page of the {{words}} with `id`. */
|
|
20
|
+
function show(id: string) {
|
|
21
|
+
return createWebRouter().fetch(
|
|
22
|
+
new URL(routes.{{pluralKey}}.show.href({ id }), "https://example.com"),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
describe("{{plural}}/show", () => {
|
|
27
|
+
test("renders the {{words}} with links to edit and delete it", async () => {
|
|
28
|
+
let row = await factories().create({{Name}}Factory);
|
|
29
|
+
|
|
30
|
+
let response = await show(row.id);
|
|
31
|
+
|
|
32
|
+
expect(response.status).toBe(200);
|
|
33
|
+
let html = await response.text();
|
|
34
|
+
expect(html).toContain(`href="${routes.{{pluralKey}}.edit.index.href({ id: row.id })}"`);
|
|
35
|
+
expect(html).toContain(`action="${routes.{{pluralKey}}.destroy.href({ id: row.id })}"`);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("answers 404 for an id no {{words}} has", async () => {
|
|
39
|
+
let response = await show("missing");
|
|
40
|
+
|
|
41
|
+
expect(response.status).toBe(404);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The {{title}} factory tests seed rows with, through the same model the app writes with. Text
|
|
3
|
+
* values carry the factory's sequence, so two {{pluralWords}} a test creates never look alike.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/data-and-background-work/data-models}
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/test-data}{{header}}
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { defineFactory } from "@sdxc/data-model/testing";
|
|
10
|
+
|
|
11
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
12
|
+
|
|
13
|
+
/** A complete {{words}}; a test overrides only the fields it is about. */
|
|
14
|
+
export const {{Name}}Factory = defineFactory({{Name}}, {
|
|
15
|
+
{{attributes}}
|
|
16
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The {{words}} form page the new and edit pages share: one field per column, filled from
|
|
3
|
+
* `values` on an edit or after a refused submission, with each refusal beside its field and the
|
|
4
|
+
* honeypot fields the page middleware checks when it is submitted.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/building-remix-apps/forms-and-params}
|
|
7
|
+
* @see {@link https://sdxc.dev/docs/building-remix-apps/interface-with-remix-ui}{{header}}
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { DocumentAssets } from "@sdxc/assets-middleware";
|
|
11
|
+
import type { I18n } from "@sdxc/i18n";
|
|
12
|
+
import type { Handle } from "remix/component";
|
|
13
|
+
|
|
14
|
+
import type { {{Name}}Row } from "~/app/models/{{name}}";
|
|
15
|
+
|
|
16
|
+
{{formImports}}
|
|
17
|
+
|
|
18
|
+
import DocumentLayout from "~/resources/layouts/document";
|
|
19
|
+
|
|
20
|
+
/** The form's fields as text, the way a submission carries them: `"true"` for a ticked box. */
|
|
21
|
+
export type {{Name}}FormValues = Readonly<Record<string, string>>;
|
|
22
|
+
|
|
23
|
+
namespace {{Name}}FormPage {
|
|
24
|
+
export interface Props {
|
|
25
|
+
/** The request's document assets, translator and URL, as every page passes them on. */
|
|
26
|
+
assets: DocumentAssets;
|
|
27
|
+
intl: I18n;
|
|
28
|
+
url: URL;
|
|
29
|
+
/** The page's title and heading. */
|
|
30
|
+
title: string;
|
|
31
|
+
/** Where the form posts: the create action, or a row's update action. */
|
|
32
|
+
action: string;
|
|
33
|
+
/** What each field starts with, by the column it edits. */
|
|
34
|
+
values: {{Name}}FormValues;
|
|
35
|
+
/** Why the last submission was refused, shown beside each field it names. */
|
|
36
|
+
issues?: ReadonlyArray<Form.Issue>;
|
|
37
|
+
/** Fresh honeypot fields from `ctx.honeypot.issue()`. */
|
|
38
|
+
honeypot: HoneypotFields.Props;
|
|
39
|
+
/** The submit button's label. */
|
|
40
|
+
submit: string;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Renders the {{words}} form inside the document layout. */
|
|
45
|
+
export function {{Name}}FormPage(handle: Handle<{{Name}}FormPage.Props>) {
|
|
46
|
+
return () => {
|
|
47
|
+
let { action, assets, honeypot, intl, issues, submit, title, url, values } = handle.props;
|
|
48
|
+
return (
|
|
49
|
+
<DocumentLayout assets={assets} intl={intl} url={url} title={title}>
|
|
50
|
+
<h1>{title}</h1>
|
|
51
|
+
<Form method="post" action={action} issues={issues}>
|
|
52
|
+
<HoneypotFields {...honeypot} />
|
|
53
|
+
{{formFields}}
|
|
54
|
+
<Button type="submit">{submit}</Button>
|
|
55
|
+
</Form>
|
|
56
|
+
</DocumentLayout>
|
|
57
|
+
);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A {{words}}'s values as the edit form shows them. */
|
|
62
|
+
export function {{key}}FormValues(row: {{Name}}Row): {{Name}}FormValues {
|
|
63
|
+
return {
|
|
64
|
+
{{rowValues}}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A submission's text fields, so a refused form shows what the visitor typed. */
|
|
69
|
+
export function submittedValues(form: FormData): {{Name}}FormValues {
|
|
70
|
+
let values: Record<string, string> = {};
|
|
71
|
+
for (let [name, value] of form) {
|
|
72
|
+
if (typeof value === "string") values[name] = value;
|
|
73
|
+
}
|
|
74
|
+
return values;
|
|
75
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Answers `create_{{snake}}`: writes a {{words}} from every field and answers it as saved, id
|
|
3
|
+
* included. A caller `{{path}}.create` refuses is a forbidden call, and a write the database
|
|
4
|
+
* refuses is a tool error carrying why.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
|
|
7
|
+
* @see {@link https://sdxc.dev/api/mcp}{{header}}
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { createTool, ForbiddenError, ToolError } from "@sdxc/mcp";
|
|
11
|
+
import { isFailure } from "@sdxc/result";
|
|
12
|
+
|
|
13
|
+
import toolset from "~/app/mcp/tools";
|
|
14
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
15
|
+
import abilities from "~/app/policies/abilities";
|
|
16
|
+
|
|
17
|
+
export default createTool(toolset.{{pluralKey}}.create, async (ctx) => {
|
|
18
|
+
if (!ctx.access.can(abilities.{{path}}.create)) {
|
|
19
|
+
throw new ForbiddenError("{{path}}.create is not permitted");
|
|
20
|
+
}
|
|
21
|
+
let created = await {{Name}}.bind({ db: ctx.db }).create(ctx.input);
|
|
22
|
+
if (isFailure(created)) throw new ToolError(created.error.message);
|
|
23
|
+
return created.data;
|
|
24
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `create_{{snake}}` tool, called over `/mcp` as a client calls it: as a member, whom
|
|
3
|
+
* `{{path}}.create` allows, and as a guest, whom it refuses.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { beforeAll, describe, expect, test } from "vitest";
|
|
10
|
+
|
|
11
|
+
import { openDatabase } from "~/app/initializers/database";
|
|
12
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
13
|
+
import { migrate } from "~/app/test/database";
|
|
14
|
+
import { callTool } from "~/app/test/mcp";
|
|
15
|
+
|
|
16
|
+
beforeAll(migrate);
|
|
17
|
+
|
|
18
|
+
describe("create_{{snake}}", () => {
|
|
19
|
+
test("writes a {{words}} and answers it with its id", async () => {
|
|
20
|
+
let result = await callTool("create_{{snake}}", { {{jsonSample}} }, "member");
|
|
21
|
+
|
|
22
|
+
expect(result.isError).toBe(false);
|
|
23
|
+
let { id } = JSON.parse(result.text) as { id: string };
|
|
24
|
+
expect(await {{Name}}.bind({ db: openDatabase() }).find(id)).not.toBeNull();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("refuses a guest, writing nothing", async () => {
|
|
28
|
+
let before = await {{Name}}.bind({ db: openDatabase() }).query().all();
|
|
29
|
+
|
|
30
|
+
let result = await callTool("create_{{snake}}", { {{jsonSample}} });
|
|
31
|
+
|
|
32
|
+
let after = await {{Name}}.bind({ db: openDatabase() }).query().all();
|
|
33
|
+
expect(result.isError).toBe(true);
|
|
34
|
+
expect(after).toHaveLength(before.length);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Answers `delete_{{snake}}`: deletes the {{words}} with the id and answers it as it was. An id
|
|
3
|
+
* no {{words}} has, or one `{{path}}.read` hides, is a tool error; a caller `{{path}}.delete`
|
|
4
|
+
* refuses is a forbidden call.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
|
|
7
|
+
* @see {@link https://sdxc.dev/api/mcp}{{header}}
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { createTool, ForbiddenError, ToolError } from "@sdxc/mcp";
|
|
11
|
+
import { isFailure } from "@sdxc/result";
|
|
12
|
+
|
|
13
|
+
import toolset from "~/app/mcp/tools";
|
|
14
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
15
|
+
import abilities from "~/app/policies/abilities";
|
|
16
|
+
|
|
17
|
+
export default createTool(toolset.{{pluralKey}}.delete, async (ctx) => {
|
|
18
|
+
let model = {{Name}}.bind({ db: ctx.db });
|
|
19
|
+
let row = await model.find(ctx.input.id);
|
|
20
|
+
if (row === null || !ctx.access.can(abilities.{{path}}.read, { {{key}}: row })) {
|
|
21
|
+
throw new ToolError(`No {{words}} has the id ${ctx.input.id}; list_{{pluralSnake}} lists them.`);
|
|
22
|
+
}
|
|
23
|
+
if (!ctx.access.can(abilities.{{path}}.delete, { {{key}}: row })) {
|
|
24
|
+
throw new ForbiddenError("{{path}}.delete is not permitted");
|
|
25
|
+
}
|
|
26
|
+
let deleted = await model.delete(ctx.input.id);
|
|
27
|
+
if (isFailure(deleted)) {
|
|
28
|
+
throw new ToolError(`No {{words}} has the id ${ctx.input.id}; list_{{pluralSnake}} lists them.`);
|
|
29
|
+
}
|
|
30
|
+
return deleted.data;
|
|
31
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `delete_{{snake}}` tool, called over `/mcp` as a client calls it: as a member, whom
|
|
3
|
+
* `{{path}}.delete` allows, and as a guest, whom it refuses.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { beforeAll, describe, expect, test } from "vitest";
|
|
10
|
+
|
|
11
|
+
import { openDatabase } from "~/app/initializers/database";
|
|
12
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
13
|
+
import { factories, migrate } from "~/app/test/database";
|
|
14
|
+
import { callTool } from "~/app/test/mcp";
|
|
15
|
+
import { {{Name}}Factory } from "~/database/factories/{{name}}";
|
|
16
|
+
|
|
17
|
+
beforeAll(migrate);
|
|
18
|
+
|
|
19
|
+
describe("delete_{{snake}}", () => {
|
|
20
|
+
test("deletes the {{words}} with the id", async () => {
|
|
21
|
+
let row = await factories().create({{Name}}Factory);
|
|
22
|
+
|
|
23
|
+
let result = await callTool("delete_{{snake}}", { id: row.id }, "member");
|
|
24
|
+
|
|
25
|
+
expect(result.isError).toBe(false);
|
|
26
|
+
expect(await {{Name}}.bind({ db: openDatabase() }).find(row.id)).toBeNull();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("reports an id no {{words}} has", async () => {
|
|
30
|
+
let result = await callTool("delete_{{snake}}", { id: "missing" }, "member");
|
|
31
|
+
|
|
32
|
+
expect(result.isError).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("refuses a guest and keeps the {{words}}", async () => {
|
|
36
|
+
let row = await factories().create({{Name}}Factory);
|
|
37
|
+
|
|
38
|
+
let result = await callTool("delete_{{snake}}", { id: row.id });
|
|
39
|
+
|
|
40
|
+
expect(result.isError).toBe(true);
|
|
41
|
+
expect(await {{Name}}.bind({ db: openDatabase() }).find(row.id)).not.toBeNull();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Answers `get_{{snake}}`: one {{words}} by its id, every field included. An id no {{words}} has,
|
|
3
|
+
* or one `{{path}}.read` hides, is a tool error pointing the model at `list_{{pluralSnake}}`.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
|
|
6
|
+
* @see {@link https://sdxc.dev/api/mcp}{{header}}
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createTool, ToolError } from "@sdxc/mcp";
|
|
10
|
+
|
|
11
|
+
import toolset from "~/app/mcp/tools";
|
|
12
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
13
|
+
import abilities from "~/app/policies/abilities";
|
|
14
|
+
|
|
15
|
+
export default createTool(toolset.{{pluralKey}}.get, async (ctx) => {
|
|
16
|
+
let row = await {{Name}}.bind({ db: ctx.db }).find(ctx.input.id);
|
|
17
|
+
if (row === null || !ctx.access.can(abilities.{{path}}.read, { {{key}}: row })) {
|
|
18
|
+
throw new ToolError(`No {{words}} has the id ${ctx.input.id}; list_{{pluralSnake}} lists them.`);
|
|
19
|
+
}
|
|
20
|
+
return row;
|
|
21
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `get_{{snake}}` tool, called over `/mcp` as a client calls it.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { beforeAll, describe, expect, test } from "vitest";
|
|
9
|
+
|
|
10
|
+
import { factories, migrate } from "~/app/test/database";
|
|
11
|
+
import { callTool } from "~/app/test/mcp";
|
|
12
|
+
import { {{Name}}Factory } from "~/database/factories/{{name}}";
|
|
13
|
+
|
|
14
|
+
beforeAll(migrate);
|
|
15
|
+
|
|
16
|
+
describe("get_{{snake}}", () => {
|
|
17
|
+
test("answers the {{words}} with the id", async () => {
|
|
18
|
+
let row = await factories().create({{Name}}Factory);
|
|
19
|
+
|
|
20
|
+
let result = await callTool("get_{{snake}}", { id: row.id });
|
|
21
|
+
|
|
22
|
+
expect(result.isError).toBe(false);
|
|
23
|
+
expect(JSON.parse(result.text)).toMatchObject({ id: row.id });
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("reports an id no {{words}} has", async () => {
|
|
27
|
+
let result = await callTool("get_{{snake}}", { id: "missing" });
|
|
28
|
+
|
|
29
|
+
expect(result.isError).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Answers `list_{{pluralSnake}}`: the newest {{pluralWords}} first, up to `limit`, so a model sees
|
|
3
|
+
* what exists before it reads one in full with `get_{{snake}}`. A {{words}} `{{path}}.read`
|
|
4
|
+
* hides is left out.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
|
|
7
|
+
* @see {@link https://sdxc.dev/api/mcp}{{header}}
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { createTool } from "@sdxc/mcp";
|
|
11
|
+
|
|
12
|
+
import toolset from "~/app/mcp/tools";
|
|
13
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
14
|
+
import abilities from "~/app/policies/abilities";
|
|
15
|
+
|
|
16
|
+
export default createTool(toolset.{{pluralKey}}.list, async (ctx) => {
|
|
17
|
+
let rows = await {{Name}}.bind({ db: ctx.db })
|
|
18
|
+
.query()
|
|
19
|
+
.orderBy("created_at", "desc")
|
|
20
|
+
.limit(ctx.input.limit)
|
|
21
|
+
.all();
|
|
22
|
+
return rows.filter((row) => ctx.access.can(abilities.{{path}}.read, { {{key}}: row }));
|
|
23
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `list_{{pluralSnake}}` tool, called over `/mcp` as a client calls it.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { beforeAll, describe, expect, test } from "vitest";
|
|
9
|
+
|
|
10
|
+
import { factories, migrate } from "~/app/test/database";
|
|
11
|
+
import { callTool } from "~/app/test/mcp";
|
|
12
|
+
import { {{Name}}Factory } from "~/database/factories/{{name}}";
|
|
13
|
+
|
|
14
|
+
beforeAll(migrate);
|
|
15
|
+
|
|
16
|
+
describe("list_{{pluralSnake}}", () => {
|
|
17
|
+
test("answers the {{pluralWords}} there are", async () => {
|
|
18
|
+
let row = await factories().create({{Name}}Factory);
|
|
19
|
+
|
|
20
|
+
let result = await callTool("list_{{pluralSnake}}", {});
|
|
21
|
+
|
|
22
|
+
expect(result.isError).toBe(false);
|
|
23
|
+
expect(result.text).toContain(row.id);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Answers `update_{{snake}}`: changes the fields the call names on the {{words}} with the id, and
|
|
3
|
+
* answers it as saved. An id no {{words}} has, one `{{path}}.read` hides, or a write the database
|
|
4
|
+
* refuses is a tool error; a caller `{{path}}.update` refuses is a forbidden call.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
|
|
7
|
+
* @see {@link https://sdxc.dev/api/mcp}{{header}}
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { NotFound } from "@sdxc/data-model";
|
|
11
|
+
import { createTool, ForbiddenError, ToolError } from "@sdxc/mcp";
|
|
12
|
+
import { isFailure } from "@sdxc/result";
|
|
13
|
+
|
|
14
|
+
import toolset from "~/app/mcp/tools";
|
|
15
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
16
|
+
import abilities from "~/app/policies/abilities";
|
|
17
|
+
|
|
18
|
+
export default createTool(toolset.{{pluralKey}}.update, async (ctx) => {
|
|
19
|
+
let { id, ...changes } = ctx.input;
|
|
20
|
+
let model = {{Name}}.bind({ db: ctx.db });
|
|
21
|
+
let row = await model.find(id);
|
|
22
|
+
if (row === null || !ctx.access.can(abilities.{{path}}.read, { {{key}}: row })) {
|
|
23
|
+
throw new ToolError(`No {{words}} has the id ${id}; list_{{pluralSnake}} lists them.`);
|
|
24
|
+
}
|
|
25
|
+
if (!ctx.access.can(abilities.{{path}}.update, { {{key}}: row })) {
|
|
26
|
+
throw new ForbiddenError("{{path}}.update is not permitted");
|
|
27
|
+
}
|
|
28
|
+
let updated = await model.update(id, changes);
|
|
29
|
+
if (isFailure(updated)) {
|
|
30
|
+
throw new ToolError(
|
|
31
|
+
updated.error instanceof NotFound
|
|
32
|
+
? `No {{words}} has the id ${id}; list_{{pluralSnake}} lists them.`
|
|
33
|
+
: updated.error.message,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
return updated.data;
|
|
37
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `update_{{snake}}` tool, called over `/mcp` as a client calls it: as a member, whom
|
|
3
|
+
* `{{path}}.update` allows, and as a guest, whom it refuses.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { beforeAll, describe, expect, test } from "vitest";
|
|
10
|
+
|
|
11
|
+
import { factories, migrate } from "~/app/test/database";
|
|
12
|
+
import { callTool } from "~/app/test/mcp";
|
|
13
|
+
import { {{Name}}Factory } from "~/database/factories/{{name}}";
|
|
14
|
+
|
|
15
|
+
beforeAll(migrate);
|
|
16
|
+
|
|
17
|
+
describe("update_{{snake}}", () => {
|
|
18
|
+
test("changes the fields it names and keeps the rest", async () => {
|
|
19
|
+
let row = await factories().create({{Name}}Factory);
|
|
20
|
+
|
|
21
|
+
let result = await callTool("update_{{snake}}", { id: row.id, {{changedJson}} }, "member");
|
|
22
|
+
|
|
23
|
+
expect(result.isError).toBe(false);
|
|
24
|
+
expect(JSON.parse(result.text)).toMatchObject({ id: row.id, {{changedJson}} });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("reports an id no {{words}} has", async () => {
|
|
28
|
+
let result = await callTool("update_{{snake}}", { id: "missing" }, "member");
|
|
29
|
+
|
|
30
|
+
expect(result.isError).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("refuses a guest", async () => {
|
|
34
|
+
let row = await factories().create({{Name}}Factory);
|
|
35
|
+
|
|
36
|
+
let result = await callTool("update_{{snake}}", { id: row.id, {{changedJson}} });
|
|
37
|
+
|
|
38
|
+
expect(result.isError).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The {{title}} model over the `{{table}}` table, with a column per field `sdxc scaffold` was
|
|
3
|
+
* given. Keep the columns in step with the migrations that change the table, and add the rules
|
|
4
|
+
* that keep a {{words}} consistent as callbacks.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/data-and-background-work/data-models}
|
|
7
|
+
* @see {@link https://sdxc.dev/api/data-model}{{header}}
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { TableRow } from "remix/data-table";
|
|
11
|
+
|
|
12
|
+
import { createModel } from "@sdxc/data-model";
|
|
13
|
+
import { generateUUID } from "@sdxc/uuid/v7";
|
|
14
|
+
import { column as c, table } from "remix/data-table";
|
|
15
|
+
|
|
16
|
+
/** The `{{table}}` table, as its migration creates it. */
|
|
17
|
+
export const {{key}}Table = table({
|
|
18
|
+
name: "{{table}}",
|
|
19
|
+
columns: {
|
|
20
|
+
id: c.text().primaryKey(),
|
|
21
|
+
{{columns}}
|
|
22
|
+
created_at: c.integer(),
|
|
23
|
+
updated_at: c.integer(),
|
|
24
|
+
},
|
|
25
|
+
/** `create` and `update` stamp `created_at` and `updated_at` with the database's clock. */
|
|
26
|
+
timestamps: true,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/** One {{words}} as it reads back from the database. */
|
|
30
|
+
export type {{Name}}Row = TableRow<typeof {{key}}Table>;
|
|
31
|
+
|
|
32
|
+
/** {{pluralTitle}}, each created with a time-ordered id; a handler reaches them as `ctx.models.{{key}}`. */
|
|
33
|
+
export const {{Name}} = createModel({{key}}Table, { generateId: generateUUID });
|
|
34
|
+
|
|
35
|
+
export default {{Name}};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The {{title}} model, writing to and reading from the `{{table}}` table its migration creates.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://sdxc.dev/docs/data-and-background-work/data-models}
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { beforeAll, describe, expect, test } from "vitest";
|
|
9
|
+
|
|
10
|
+
import { openDatabase } from "~/app/initializers/database";
|
|
11
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
12
|
+
import { factories, migrate } from "~/app/test/database";
|
|
13
|
+
import { {{Name}}Factory } from "~/database/factories/{{name}}";
|
|
14
|
+
|
|
15
|
+
beforeAll(migrate);
|
|
16
|
+
|
|
17
|
+
describe("{{Name}}", () => {
|
|
18
|
+
test("reads back a {{words}} it created", async () => {
|
|
19
|
+
let created = await factories().create({{Name}}Factory);
|
|
20
|
+
|
|
21
|
+
let found = await {{Name}}.bind({ db: openDatabase() }).find(created.id);
|
|
22
|
+
|
|
23
|
+
expect(found).toMatchObject({ id: created.id });
|
|
24
|
+
});
|
|
25
|
+
});
|