create-mana 0.0.0-stage → 0.1.0

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.
Files changed (101) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +28 -2
  3. package/bin/create-mana.mjs +5 -0
  4. package/kits/auth/files/src/api/auth.ts +100 -0
  5. package/kits/auth/files/src/api/profile.ts +48 -0
  6. package/kits/auth/files/src/api/types.ts +28 -0
  7. package/kits/auth/files/src/components/AppLayout.mana +31 -0
  8. package/kits/auth/files/src/components/VerifyBanner.mana +47 -0
  9. package/kits/auth/files/src/router/routes.ts +31 -0
  10. package/kits/auth/files/src/shared/events.ts +11 -0
  11. package/kits/auth/files/src/stores/session.ts +99 -0
  12. package/kits/auth/files/src/views/ForgotPassword.mana +83 -0
  13. package/kits/auth/files/src/views/Home.mana +15 -0
  14. package/kits/auth/files/src/views/Profile.mana +172 -0
  15. package/kits/auth/files/src/views/Register.mana +86 -0
  16. package/kits/auth/files/src/views/ResetPassword.mana +90 -0
  17. package/kits/auth/files/src/views/SignIn.mana +84 -0
  18. package/kits/auth/files/src/views/VerifyEmail.mana +78 -0
  19. package/kits/auth/files/tests/auth.test.mjs +93 -0
  20. package/kits/auth/files/tests/home.test.mjs +38 -0
  21. package/kits/auth/files/tests/profile.test.mjs +68 -0
  22. package/kits/auth/files/tests/support/users.mjs +2 -0
  23. package/kits/auth/kit.json +80 -0
  24. package/kits/base/files/README.md +39 -0
  25. package/kits/base/kit.json +7 -0
  26. package/kits/organisations/files/src/api/auth.ts +124 -0
  27. package/kits/organisations/files/src/api/members.ts +243 -0
  28. package/kits/organisations/files/src/api/notifications.ts +63 -0
  29. package/kits/organisations/files/src/api/organisations.ts +41 -0
  30. package/kits/organisations/files/src/api/scope.ts +98 -0
  31. package/kits/organisations/files/src/api/types.ts +72 -0
  32. package/kits/organisations/files/src/assets/styles/organisations.css +46 -0
  33. package/kits/organisations/files/src/components/AppLayout.mana +90 -0
  34. package/kits/organisations/files/src/components/NotificationBell.mana +35 -0
  35. package/kits/organisations/files/src/components/NotificationList.mana +112 -0
  36. package/kits/organisations/files/src/components/OrganisationSwitcher.mana +171 -0
  37. package/kits/organisations/files/src/composables/notifications.ts +28 -0
  38. package/kits/organisations/files/src/router/routes.ts +43 -0
  39. package/kits/organisations/files/src/shared/roles.ts +10 -0
  40. package/kits/organisations/files/src/stores/notifications.ts +41 -0
  41. package/kits/organisations/files/src/stores/session.ts +158 -0
  42. package/kits/organisations/files/src/utilities/format.ts +15 -0
  43. package/kits/organisations/files/src/views/Forbidden.mana +28 -0
  44. package/kits/organisations/files/src/views/Home.mana +15 -0
  45. package/kits/organisations/files/src/views/Invite.mana +156 -0
  46. package/kits/organisations/files/src/views/InviteMember.mana +99 -0
  47. package/kits/organisations/files/src/views/Members.mana +218 -0
  48. package/kits/organisations/files/src/views/NoOrganisation.mana +184 -0
  49. package/kits/organisations/files/src/views/Notifications.mana +30 -0
  50. package/kits/organisations/files/src/views/OrganisationForm.mana +76 -0
  51. package/kits/organisations/files/src/views/OrganisationSettings.mana +62 -0
  52. package/kits/organisations/files/src/views/Profile.mana +213 -0
  53. package/kits/organisations/files/src/views/Register.mana +113 -0
  54. package/kits/organisations/files/tests/auth.test.mjs +82 -0
  55. package/kits/organisations/files/tests/home.test.mjs +43 -0
  56. package/kits/organisations/files/tests/members.test.mjs +89 -0
  57. package/kits/organisations/files/tests/notifications.test.mjs +49 -0
  58. package/kits/organisations/files/tests/organisations.test.mjs +70 -0
  59. package/kits/organisations/files/tests/profile.test.mjs +79 -0
  60. package/kits/organisations/files/tests/support/users.mjs +3 -0
  61. package/kits/organisations/kit.json +51 -0
  62. package/lib/compose.mjs +209 -0
  63. package/lib/new.mjs +161 -0
  64. package/lib/prompts.mjs +182 -0
  65. package/package.json +36 -4
  66. package/template/.nvmrc +1 -0
  67. package/template/README.md +23 -0
  68. package/template/_gitignore +25 -0
  69. package/template/index.html +17 -0
  70. package/template/package.json +22 -0
  71. package/template/public/favicon.svg +1 -0
  72. package/template/public/icons.svg +24 -0
  73. package/template/src/App.mana +15 -0
  74. package/template/src/api/ApiError.ts +17 -0
  75. package/template/src/api/client.ts +116 -0
  76. package/template/src/assets/styles/main.css +158 -0
  77. package/template/src/components/AppLogo.mana +20 -0
  78. package/template/src/components/Button.mana +25 -0
  79. package/template/src/components/ConfirmHost.mana +43 -0
  80. package/template/src/components/TextField.mana +60 -0
  81. package/template/src/components/ToastHost.mana +40 -0
  82. package/template/src/composables/media.ts +19 -0
  83. package/template/src/composables/unsaved.ts +29 -0
  84. package/template/src/main.ts +25 -0
  85. package/template/src/router/index.ts +14 -0
  86. package/template/src/router/routes.ts +11 -0
  87. package/template/src/stores/confirm.ts +45 -0
  88. package/template/src/stores/toasts.ts +64 -0
  89. package/template/src/utilities/query.ts +40 -0
  90. package/template/src/utilities/timers.ts +12 -0
  91. package/template/src/views/Home.mana +12 -0
  92. package/template/src/views/NotFound.mana +13 -0
  93. package/template/tests/home.test.mjs +29 -0
  94. package/template/tests/support/api.mjs +44 -0
  95. package/template/tests/support/check.mjs +15 -0
  96. package/template/tests/support/dom.mjs +45 -0
  97. package/template/tests/support/paths.mjs +54 -0
  98. package/template/tests/support/settle.mjs +15 -0
  99. package/template/tsconfig.json +35 -0
  100. package/template/vite.config.ts +29 -0
  101. package/versions.json +3 -0
@@ -0,0 +1,213 @@
1
+ <script setup lang="ts">
2
+ /*
3
+ * Settings › Profile: /profile ("profile"), for
4
+ * everyone.
5
+ *
6
+ * Your details: your name, and your email, which changes only once a link
7
+ * sent to the new address is opened; until then the old one signs in,
8
+ * and the page says what's waiting, with Resend and Cancel the change. Save
9
+ * changes is on once something changed; what the API answers replaces the
10
+ * session's user, so the top bar follows, and "Saved." shows (toasts are
11
+ * for errors, A4).
12
+ * Password: the current one, a new one and its confirmation, checked as on
13
+ * sign-up, the API having the last word.
14
+ * Your organisations, with your role in each and Leave for those you don't
15
+ * own, confirmed first; leaving the one you're in moves you to another
16
+ * (C19 starts its pages afresh).
17
+ * Leaving the page with something typed and not saved asks first.
18
+ */
19
+ import { computed, reactive, ref, watch } from "@mana-js/mana";
20
+
21
+ import ApiError from "@/api/ApiError";
22
+ import * as members from "@/api/members";
23
+ import * as profile from "@/api/profile";
24
+ import type { Membership } from "@/api/types";
25
+ import TextField from "@/components/TextField.mana";
26
+ import { useUnsavedChanges } from "@/composables/unsaved";
27
+ import { ROLES } from "@/shared/roles";
28
+ import { useConfirm } from "@/stores/confirm";
29
+ import { useSession } from "@/stores/session";
30
+ import { useToasts } from "@/stores/toasts";
31
+
32
+ defineOptions({ name: "Profile" });
33
+
34
+ const sessionStore = useSession();
35
+ const confirmStore = useConfirm();
36
+ const toastStore = useToasts();
37
+
38
+ const details = reactive({ name: "", email: "" });
39
+ const detailErrors = reactive<{ name?: string; email?: string }>({});
40
+ const savingDetails = ref(false);
41
+ const detailsSaved = ref(false);
42
+ const emailSent = ref<string | null>(null);
43
+
44
+ const password = reactive({ current: "", password: "", confirm: "" });
45
+ const passwordErrors = reactive<{ current?: string; password?: string; confirm?: string }>({});
46
+ const savingPassword = ref(false);
47
+ const passwordSaved = ref(false);
48
+
49
+ // The form starts from the user, and again when they change (saved, or confirmed elsewhere).
50
+ watch(() => sessionStore.user, user => {
51
+ details.name = user?.name ?? "";
52
+ details.email = user?.email ?? "";
53
+ }, { immediate: true });
54
+
55
+ const detailsChanged = computed(() => details.name != (sessionStore.user?.name ?? "") || details.email.trim().toLowerCase() != (sessionStore.user?.email ?? ""));
56
+ const passwordTyped = computed(() => !!(password.current || password.password || password.confirm));
57
+
58
+ useUnsavedChanges(() => detailsChanged.value || passwordTyped.value);
59
+
60
+ // A new change hides "Saved.".
61
+ watch(() => [details.name, details.email], () => detailsSaved.value = false);
62
+ watch(passwordTyped, typed => {
63
+ if(typed)
64
+ passwordSaved.value = false;
65
+ });
66
+
67
+ function failed(title: string, error: unknown) {
68
+ toastStore.error(title, error instanceof ApiError ? error.message : "Something went wrong. Try again.");
69
+ }
70
+
71
+ async function saveDetails() {
72
+ detailErrors.name = details.name.trim() ? undefined : "Enter your name.";
73
+ detailErrors.email = details.email.trim() ? undefined : "Enter your email.";
74
+
75
+ if(detailErrors.name || detailErrors.email || savingDetails.value)
76
+ return;
77
+
78
+ const email = details.email.trim().toLowerCase();
79
+ const newEmail = email != sessionStore.user?.email;
80
+
81
+ savingDetails.value = true;
82
+
83
+ try {
84
+ sessionStore.receive(await profile.updateProfile({ name: details.name, email }));
85
+ emailSent.value = newEmail ? email : null;
86
+ detailsSaved.value = true;
87
+ } catch(error) {
88
+ if(error instanceof ApiError && (error.field == "name" || error.field == "email"))
89
+ detailErrors[error.field] = error.message;
90
+ else
91
+ failed("Couldn't save your details", error);
92
+ } finally {
93
+ savingDetails.value = false;
94
+ }
95
+ }
96
+
97
+ async function resend() {
98
+ try {
99
+ await profile.resendEmailChange();
100
+ emailSent.value = sessionStore.user?.pendingEmail ?? null;
101
+ } catch(error) {
102
+ failed("Couldn't send the link", error);
103
+ }
104
+ }
105
+
106
+ async function cancelChange() {
107
+ try {
108
+ sessionStore.receive(await profile.cancelEmailChange());
109
+ emailSent.value = null;
110
+ } catch(error) {
111
+ failed("Couldn't cancel the change", error);
112
+ }
113
+ }
114
+
115
+ async function savePassword() {
116
+ passwordErrors.current = password.current ? undefined : "Enter your current password.";
117
+ passwordErrors.password = password.password.length >= 8 ? undefined : "Use at least 8 characters.";
118
+ passwordErrors.confirm = password.confirm == password.password ? undefined : "The passwords don't match.";
119
+
120
+ if(passwordErrors.current || passwordErrors.password || passwordErrors.confirm || savingPassword.value)
121
+ return;
122
+
123
+ savingPassword.value = true;
124
+
125
+ try {
126
+ await profile.changePassword({ current: password.current, password: password.password });
127
+ password.current = password.password = password.confirm = "";
128
+ passwordSaved.value = true;
129
+ } catch(error) {
130
+ if(error instanceof ApiError && (error.field == "current" || error.field == "password"))
131
+ passwordErrors[error.field] = error.message;
132
+ else
133
+ failed("Couldn't change your password", error);
134
+ } finally {
135
+ savingPassword.value = false;
136
+ }
137
+ }
138
+
139
+ async function leave(organisation: Membership) {
140
+ const yes = await confirmStore.ask({
141
+ title: `Leave ${organisation.name}?`,
142
+ text: "You lose access to it at once. To come back, you'd need a new invitation.",
143
+ confirm: "Leave",
144
+ danger: true,
145
+ });
146
+
147
+ if(!yes)
148
+ return;
149
+
150
+ try {
151
+ sessionStore.receive(await members.leaveOrganisation(organisation.id));
152
+ } catch(error) {
153
+ failed(`Couldn't leave ${organisation.name}`, error);
154
+ }
155
+ }
156
+ </script>
157
+
158
+ <template>
159
+ <div class="page-head"><h1>Profile</h1></div>
160
+
161
+ <form class="card form-narrow" novalidate aria-labelledby="details-title" :class="{ 'is-submitting': savingDetails }" @submit.prevent="saveDetails">
162
+ <div class="card-head"><h2 id="details-title">Your details</h2></div>
163
+ <TextField id="profile-name" label="Name" autocomplete="name" :error="detailErrors.name" m-model="details.name" />
164
+ <TextField id="profile-email" type="email" label="Email" autocomplete="email" hint="Changing it sends a link to the new address; it changes once you open it." :error="detailErrors.email" m-model="details.email" />
165
+
166
+ <div class="pending" role="status" m-if="sessionStore.user?.pendingEmail">
167
+ <p m-if="emailSent">We sent a link to <b>{{ emailSent }}</b>. Your email changes once you open it; until then you sign in with {{ sessionStore.user?.email }}.</p>
168
+ <p m-else>Waiting for you to confirm <b>{{ sessionStore.user?.pendingEmail }}</b>. Until you open the link we sent there, you sign in with {{ sessionStore.user?.email }}.</p>
169
+ <span class="inline-actions">
170
+ <button type="button" class="link-button" @click="resend()">Resend the link</button>
171
+ <button type="button" class="link-button" @click="cancelChange()">Cancel the change</button>
172
+ </span>
173
+ </div>
174
+
175
+ <div class="form-foot">
176
+ <button type="submit" class="btn primary" :disabled="!detailsChanged || savingDetails">{{ savingDetails ? "Saving…" : "Save changes" }}</button>
177
+ <span class="saved" role="status" m-if="detailsSaved && !detailsChanged">Saved.</span>
178
+ </div>
179
+ </form>
180
+
181
+ <form class="card form-narrow" novalidate aria-labelledby="password-title" :class="{ 'is-submitting': savingPassword }" @submit.prevent="savePassword">
182
+ <div class="card-head"><h2 id="password-title">Password</h2></div>
183
+ <TextField id="profile-current" type="password" label="Current password" autocomplete="current-password" :error="passwordErrors.current" m-model="password.current" />
184
+ <TextField id="profile-password" type="password" label="New password" autocomplete="new-password" hint="At least 8 characters." :error="passwordErrors.password" m-model="password.password" />
185
+ <TextField id="profile-confirm" type="password" label="Confirm the new password" autocomplete="new-password" :error="passwordErrors.confirm" m-model="password.confirm" />
186
+ <div class="form-foot">
187
+ <button type="submit" class="btn" :disabled="!passwordTyped || savingPassword">{{ savingPassword ? "Changing…" : "Change password" }}</button>
188
+ <span class="saved" role="status" m-if="passwordSaved && !passwordTyped">Password changed.</span>
189
+ </div>
190
+ </form>
191
+
192
+ <section class="card form-narrow" aria-labelledby="organisations-title">
193
+ <div class="card-head"><h2 id="organisations-title">Your organisations</h2></div>
194
+ <table class="organisations">
195
+ <tbody>
196
+ <tr m-for="organisation in sessionStore.user?.organisations ?? []" :key="organisation.id">
197
+ <td>{{ organisation.name }}<span class="tag" m-if="organisation.id == sessionStore.organisation?.id">Current</span></td>
198
+ <td>{{ ROLES[organisation.role].label }}</td>
199
+ <td class="actions">
200
+ <span class="muted" m-if="organisation.role == 'owner'">You own it</span>
201
+ <button type="button" class="link-button danger" m-else @click="leave(organisation)">Leave</button>
202
+ </td>
203
+ </tr>
204
+ </tbody>
205
+ </table>
206
+ </section>
207
+ </template>
208
+
209
+ <style scoped>
210
+ .pending { margin: 0 0 14px; padding: 8px 10px; border: 1.5px dashed var(--ink); border-radius: 3px; font-size: 13px; }
211
+ .pending p { margin: 0 0 4px; }
212
+ .inline-actions { display: inline-flex; flex-wrap: wrap; gap: 12px; font-size: 13px; }
213
+ </style>
@@ -0,0 +1,113 @@
1
+ <script setup lang="ts">
2
+ /*
3
+ * The Register page: a name, an email, a password of 8 characters or more and its confirmation. The API's answer about a field shows on it, anything else as a toast; signed in, the user lands on the
4
+ * home page, where a banner asks them to confirm their email (a link was mailed).
5
+ */
6
+ import { computed, reactive, ref, resource, useRoute, useRouter } from "@mana-js/mana";
7
+
8
+ import ApiError from "@/api/ApiError";
9
+ import * as members from "@/api/members";
10
+ import AppLogo from "@/components/AppLogo.mana";
11
+ import Button from "@/components/Button.mana";
12
+ import TextField from "@/components/TextField.mana";
13
+ import { ROLES } from "@/shared/roles";
14
+ import { useSession } from "@/stores/session";
15
+ import { useToasts } from "@/stores/toasts";
16
+
17
+ type RegisterField = "name" | "email" | "organisation" | "password" | "confirm";
18
+
19
+ const route = useRoute();
20
+ const router = useRouter();
21
+ const sessionStore = useSession();
22
+ const toastStore = useToasts();
23
+ const form = reactive({ name: "", email: "", organisation: "", password: "", confirm: "" });
24
+ const errors = reactive<Partial<Record<RegisterField, string>>>({});
25
+ const submitting = ref(false);
26
+
27
+ // An invitation (/register?invite=…): its email is the account's, and its organisation the one joined.
28
+ const invite = computed(() => route.query.invite ?? null);
29
+ const invitation = resource(() => invite.value ? members.invitation(invite.value) : null);
30
+ const joining = computed(() => invitation.data != null && !invitation.data.expired);
31
+ const roleText = computed(() => {
32
+ const role = invitation.data?.role;
33
+
34
+ return role ? `${role == "admin" ? "an" : "a"} ${ROLES[role].label.toLowerCase()}` : "";
35
+ });
36
+
37
+ // A field of the form's, as the API's errors name them (src/api/auth.ts gives them the form's names).
38
+ function isField(field: string | null): field is RegisterField {
39
+ return field == "name" || field == "email" || field == "organisation" || field == "password" || field == "confirm";
40
+ }
41
+
42
+ function validate() {
43
+ errors.name = form.name.trim() ? undefined : "Enter your full name.";
44
+ if(joining.value && invitation.data)
45
+ form.email = invitation.data.email;
46
+
47
+ errors.email = !form.email ? "Enter your email." : !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(form.email) ? "Enter an email like name@company.com." : undefined;
48
+ errors.organisation = joining.value || form.organisation.trim() ? undefined : "Enter your organisation's name.";
49
+ errors.password = form.password.length >= 8 ? undefined : "Use at least 8 characters.";
50
+ errors.confirm = !form.confirm ? "Enter your password again." : form.confirm != form.password ? "The passwords don't match." : undefined;
51
+
52
+ return Object.values(errors).every(error => !error);
53
+ }
54
+
55
+ async function submit() {
56
+ if(!validate())
57
+ return;
58
+
59
+ submitting.value = true;
60
+ toastStore.clear();
61
+
62
+ try {
63
+ await sessionStore.register({ name: form.name.trim(), email: form.email, organisation: form.organisation.trim(), password: form.password, passwordConfirmation: form.confirm, invitation: joining.value ? invite.value : null });
64
+ } catch(error) {
65
+ if(error instanceof ApiError && isField(error.field))
66
+ errors[error.field] = error.message;
67
+ else
68
+ toastStore.error("Couldn't create your account", error instanceof ApiError ? error.message : "Something went wrong. Try again.");
69
+
70
+ return;
71
+ } finally {
72
+ submitting.value = false;
73
+ }
74
+
75
+ await router.replace({ name: "dashboard" });
76
+ }
77
+ </script>
78
+
79
+ <template>
80
+ <div class="auth">
81
+ <AppLogo />
82
+
83
+ <h1>Create your account</h1>
84
+
85
+ <p class="lead" m-if="joining">To join {{ invitation.data?.organisation }} as {{ roleText }}, as {{ invitation.data?.invitedBy }} invited you.</p>
86
+ <div class="alert" role="alert" m-else-if="invite && (invitation.error || invitation.data?.expired)">
87
+ {{ invitation.data?.expired ? "This invitation has expired." : "This invitation isn't valid any more." }}
88
+ You can still create an account with an organisation of your own.
89
+ </div>
90
+
91
+ <form novalidate :class="{ 'is-submitting': submitting }" :aria-busy="submitting" @submit.prevent="submit">
92
+ <TextField id="register-name" name="name" label="Full name" autocomplete="name" :error="errors.name" m-model="form.name" />
93
+ <div class="field" m-if="joining">
94
+ <span class="label">Email</span>
95
+ <p class="fixed-value">{{ invitation.data?.email }}</p>
96
+ <p class="error" m-if="errors.email">{{ errors.email }}</p>
97
+ </div>
98
+ <TextField id="register-email" name="email" label="Email" type="email" autocomplete="email" placeholder="name@company.com" :error="errors.email" m-model="form.email" m-else />
99
+ <TextField id="register-organisation" name="organisation" label="Organisation name" autocomplete="organization" :error="errors.organisation" m-model="form.organisation" m-if="!joining" />
100
+ <TextField id="register-password" name="password" label="Password" type="password" autocomplete="new-password" hint="At least 8 characters." :error="errors.password" m-model="form.password" />
101
+ <TextField id="register-confirm" name="confirm" label="Confirm password" type="password" autocomplete="new-password" :error="errors.confirm" m-model="form.confirm" />
102
+
103
+ <div class="submit">
104
+ <Button type="submit" :label="submitting ? 'Creating account…' : 'Create account'" :disabled="submitting" />
105
+ </div>
106
+ </form>
107
+
108
+ <p class="switch">
109
+ Already have an account?
110
+ <RouterLink to="/login">Sign in</RouterLink>
111
+ </p>
112
+ </div>
113
+ </template>
@@ -0,0 +1,82 @@
1
+ // Signing in, registering (with an organisation of one's own, or by an invitation's link), the guards and signing out, through the real pages and router against a faked API.
2
+ import { check } from "./support/check.mjs";
3
+ import { settle } from "./support/settle.mjs";
4
+ import { useDom } from "./support/dom.mjs";
5
+ import { useFakeApi } from "./support/api.mjs";
6
+ import { apiUser } from "./support/users.mjs";
7
+
8
+ const w = useDom('<main id="app"></main>', { url: "http://app.test/login" });
9
+ const api = useFakeApi();
10
+ let signedIn = false;
11
+ let user = apiUser();
12
+
13
+ api.get("/api/user", () => signedIn ? { json: user } : { status: 401, json: { message: "Unauthenticated." } });
14
+ api.get("/api/user/notifications", () => ({ json: { notifications: [], total: 0, unread: 0, page: 1, page_size: 25 } }));
15
+ api.post("/api/login", body => body.password == "password" ? (signedIn = true, { json: user }) : { status: 422, json: { message: "Wrong email or password.", errors: { email: ["Wrong email or password."] } } });
16
+ api.post("/api/logout", () => (signedIn = false, { status: 204 }));
17
+ api.post("/api/register", body => body.email == "taken@example.test"
18
+ ? { status: 422, json: { message: "An account with this email already exists.", errors: { email: ["An account with this email already exists."] } } }
19
+ : (signedIn = true, user = body.invitation
20
+ ? apiUser({ name: body.name, email: "yaw@example.test", verified: true, current_organisation_id: 2, organisations: [{ id: 2, name: "Bakery", role: "member" }] })
21
+ : apiUser({ name: body.name, email: body.email, verified: false, current_organisation_id: 3, organisations: [{ id: 3, name: body.organisation, role: "owner" }] }), { status: 201, json: user }));
22
+ api.get("/api/invitations/abc", () => ({ json: { email: "yaw@example.test", role: "member", organisation: "Bakery", invited_by: "Esi", expired: false } }));
23
+
24
+ const { createApp, createEvents, createStores } = await import("@mana-js/mana");
25
+ const { default: App } = await import("@/App.mana");
26
+ const { default: router } = await import("@/router");
27
+ const { useSession } = await import("@/stores/session");
28
+ let session;
29
+ const Probe = { setup() { session = useSession(); return {}; }, template: `<i></i>` };
30
+ const events = createEvents();
31
+
32
+ w.sessionStorage.clear();
33
+ createApp({ components: { App, Probe }, template: `<div><App /><Probe /></div>` }).use(createStores()).use(events).use(router).mount("#app");
34
+ events.on("signedOut", () => void router.replace({ name: "login" }));
35
+ await router.isReady();
36
+
37
+ const root = document.getElementById("app");
38
+ const $ = s => root.querySelector(s);
39
+ const text = s => $(s)?.textContent.replace(/\s+/g, " ").trim() ?? null;
40
+ const type = (id, value) => { const input = $(id); input.value = value; input.dispatchEvent(new w.Event("input")); };
41
+ const click = el => el.dispatchEvent(new w.MouseEvent("click", { bubbles: true, cancelable: true }));
42
+ const button = label => [...root.querySelectorAll("button")].find(b => b.textContent.trim() == label);
43
+ const go = async path => { await router.push(path); await settle(); };
44
+ const wait = async () => { await new Promise(resolve => setTimeout(resolve, 30)); await settle(); };
45
+
46
+ await settle();
47
+ check("signed out: the sign-in page, and a signed-in page goes to it", w.location.pathname == "/login" && /^Sign in to /.test(text("h1")) && (await go("/"), w.location.pathname == "/login"));
48
+
49
+ type("#login-email", "ama@example.test"); type("#login-password", "wrong");
50
+ click(button("Sign in")); await wait();
51
+ check("a wrong password: told so, and still on the sign-in page", w.location.pathname == "/login" && document.body.textContent.includes("Wrong email or password."));
52
+ type("#login-password", "password");
53
+ click(button("Sign in")); await wait();
54
+ check("the right one: signed in, on the home page, in Acme", w.location.pathname == "/" && /Welcome, Ama Owusu/.test(text("h1")) && /Acme/.test(text(".topbar") ?? ""));
55
+ click(button("Sign out")); await wait();
56
+ check("signing out lands on the sign-in page, the user gone", session.user === null && w.location.pathname == "/login");
57
+
58
+ // Registering: an organisation of one's own.
59
+ await go("/register");
60
+ check("the register page asks for an organisation too", ["#register-name", "#register-email", "#register-organisation", "#register-password", "#register-confirm"].every(id => $(id)));
61
+ click(button("Create account")); await settle();
62
+ check("empty: it says what's wrong, the organisation's name too, and sends nothing", text("#register-organisation-error") != null && api.calls.every(call => call.path != "/api/register"));
63
+ type("#register-name", "Kofi Boateng"); type("#register-email", "kofi@example.test"); type("#register-organisation", "Kofi's Shop"); type("#register-password", "longenough"); type("#register-confirm", "longenough");
64
+ click(button("Create account")); await wait();
65
+ const made = api.calls.find(call => call.path == "/api/register");
66
+ check("registered: sent with the organisation, signed in, working in it as its owner, and the banner asks to confirm the email", made?.body.organisation == "Kofi's Shop" && !made.body.invitation && /working in Kofi's Shop as owner/.test(text("main") ?? "") && /Confirm your email/.test(text(".banner") ?? ""));
67
+
68
+ // Joining by an invitation's link.
69
+ click(button("Sign out")); await wait();
70
+ await go("/register?invite=abc");
71
+ await wait();
72
+ check("an invitation's link: who invited them, to what, the email fixed and no organisation asked for", /To join Bakery as a member, as Esi invited you/.test(text(".lead") ?? "") && text(".fixed-value") == "yaw@example.test" && !$("#register-organisation"));
73
+ type("#register-name", "Yaw Asante"); type("#register-password", "longenough"); type("#register-confirm", "longenough");
74
+ click(button("Create account")); await wait();
75
+ const joined = api.calls.filter(call => call.path == "/api/register").at(-1);
76
+ check("registered by it: the invitation's token sent, not an organisation; they work in the one they joined, as a member", joined.body.invitation == "abc" && !joined.body.organisation && /working in Bakery as member/.test(text("main") ?? ""));
77
+
78
+ // The guard for someone in none.
79
+ user = apiUser({ current_organisation_id: null, organisations: [] });
80
+ await session.load();
81
+ await go("/profile");
82
+ check("someone in no organisation is sent to the page for it", w.location.pathname == "/no-organisation");
@@ -0,0 +1,43 @@
1
+ // The home page, signed in: it greets the user by name and says where they work and as what; the frame offers Members and Settings to those who manage the organisation and not to a member; and an
2
+ // address with no page is said so.
3
+ import { check } from "./support/check.mjs";
4
+ import { settle } from "./support/settle.mjs";
5
+ import { useDom } from "./support/dom.mjs";
6
+ import { useFakeApi } from "./support/api.mjs";
7
+ import { apiUser } from "./support/users.mjs";
8
+
9
+ useDom('<main id="app"></main>', { url: "http://app.test/" });
10
+
11
+ const api = useFakeApi();
12
+ let user = apiUser();
13
+
14
+ api.get("/api/user", () => ({ json: user }));
15
+ api.get("/api/user/notifications", () => ({ json: { notifications: [], total: 0, unread: 0, page: 1, page_size: 25 } }));
16
+
17
+ const { createApp, createEvents, createStores } = await import("@mana-js/mana");
18
+ const { default: App } = await import("@/App.mana");
19
+ const { default: router } = await import("@/router");
20
+ const { useSession } = await import("@/stores/session");
21
+ let session;
22
+ const Probe = { setup() { session = useSession(); return {}; }, template: `<i></i>` };
23
+
24
+ createApp({ components: { App, Probe }, template: `<div><App /><Probe /></div>` }).use(createStores()).use(createEvents()).use(router).mount("#app");
25
+ await session.load();
26
+ await router.isReady();
27
+ await router.push("/");
28
+ await settle();
29
+
30
+ const root = document.getElementById("app");
31
+ const text = selector => root.querySelector(selector)?.textContent.replace(/\s+/g, " ").trim() ?? null;
32
+
33
+ check("the home page: a greeting, the organisation they work in and their role", text("h1") == "Welcome, Ama Owusu" && /working in Acme as owner/.test(text("main") ?? ""));
34
+ check("the owner's frame: the organisation switcher, Members and Settings, their name and Sign out", /Acme/.test(text(".topbar") ?? "") && /Members/.test(text(".nav") ?? "") && /Settings/.test(text(".nav") ?? "") && /Sign out/.test(text(".topbar") ?? ""));
35
+
36
+ user = apiUser({ organisations: [{ id: 1, name: "Acme", role: "member" }] });
37
+ await session.load();
38
+ await settle();
39
+ check("a member's frame has no Members or Settings", !root.querySelector(".nav"));
40
+
41
+ await router.push("/nowhere/at/all");
42
+ await settle();
43
+ check("an address with no page: not found, with the way back", text("h1") == "Page not found" && root.querySelector("a[href='/']") !== null);
@@ -0,0 +1,89 @@
1
+ // Members and invitations, for those who manage the organisation: the list with each one's role, a role changed, someone removed, ownership handed to an admin; inviting by email, and resending or
2
+ // revoking what's waiting; the organisation's settings; and what a member is told when they ask for a page that isn't theirs.
3
+ import { check } from "./support/check.mjs";
4
+ import { settle } from "./support/settle.mjs";
5
+ import { useDom } from "./support/dom.mjs";
6
+ import { useFakeApi } from "./support/api.mjs";
7
+ import { acme, apiUser } from "./support/users.mjs";
8
+
9
+ const w = useDom('<main id="app"></main>', { url: "http://app.test/members" });
10
+ const api = useFakeApi();
11
+ let user = apiUser();
12
+ let people = [{ id: 1, name: "Ama Owusu", email: "ama@example.test", role: "owner", you: true }, { id: 2, name: "Kofi Boateng", email: "kofi@example.test", role: "admin", you: false }, { id: 3, name: "Esi Mensa", email: "esi@example.test", role: "member", you: false }];
13
+ let waiting = [{ id: 7, email: "yaw@example.test", role: "member", invited_by: "Ama Owusu", sent_at: "2026-11-01T10:00:00Z", expires_at: "2026-11-08T10:00:00Z" }];
14
+ let name = "Acme";
15
+
16
+ api.get("/api/user", () => ({ json: user }));
17
+ api.get("/api/user/notifications", () => ({ json: { notifications: [], total: 0, unread: 0, page: 1, page_size: 25 } }));
18
+ api.get("/api/organisations/1/members", () => ({ json: people }));
19
+ api.get("/api/organisations/1/invitations", () => ({ json: waiting }));
20
+ api.patch("/api/organisations/1/members/3", body => (people = people.map(one => one.id == 3 ? { ...one, role: body.role } : one), { json: people[2] }));
21
+ api.delete("/api/organisations/1/members/3", () => (people = people.filter(one => one.id != 3), { status: 204 }));
22
+ api.post("/api/organisations/1/members/2/ownership", () => (user = apiUser({ organisations: [{ ...acme, role: "admin" }] }), { json: user }));
23
+ api.post("/api/organisations/1/invitations", body => (waiting = [...waiting, { id: 8, email: body.email, role: body.role, invited_by: "Ama Owusu", sent_at: "2026-11-02T10:00:00Z", expires_at: "2026-11-09T10:00:00Z" }], { status: 201, json: waiting.at(-1) }));
24
+ api.post("/api/organisations/1/invitations/7/resend", () => ({ status: 204 }));
25
+ api.delete("/api/organisations/1/invitations/7", () => (waiting = waiting.filter(one => one.id != 7), { status: 204 }));
26
+ api.get("/api/organisations/1/settings", () => ({ json: { name } }));
27
+ api.patch("/api/organisations/1/settings", body => (name = body.name, user = apiUser({ organisations: [{ ...acme, name }] }), { json: { name } }));
28
+
29
+ const { createApp, createEvents, createStores } = await import("@mana-js/mana");
30
+ const { default: App } = await import("@/App.mana");
31
+ const { default: router } = await import("@/router");
32
+ const { useSession } = await import("@/stores/session");
33
+ let session;
34
+ const Probe = { setup() { session = useSession(); return {}; }, template: `<i></i>` };
35
+
36
+ createApp({ components: { App, Probe }, template: `<div><App /><Probe /></div>` }).use(createStores()).use(createEvents()).use(router).mount("#app");
37
+ await session.load();
38
+ await router.isReady();
39
+ await router.push("/members");
40
+ await wait0();
41
+
42
+ function wait0() { return new Promise(resolve => setTimeout(resolve, 50)).then(() => settle()); }
43
+
44
+ const root = document.getElementById("app");
45
+ const $ = s => root.querySelector(s);
46
+ const text = s => $(s)?.textContent.replace(/\s+/g, " ").trim() ?? null;
47
+ const type = (id, value) => { const input = $(id); input.value = value; input.dispatchEvent(new w.Event("input")); };
48
+ const click = el => el.dispatchEvent(new w.MouseEvent("click", { bubbles: true, cancelable: true }));
49
+ const button = label => [...root.querySelectorAll("button")].find(b => b.textContent.trim() == label);
50
+ const wait = async () => { await new Promise(resolve => setTimeout(resolve, 30)); await settle(); };
51
+
52
+ const rows = () => [...root.querySelectorAll("table.members")][0] ? [...[...root.querySelectorAll("table.members")][0].querySelectorAll("tbody tr")].map(row => row.textContent.replace(/\s+/g, " ").trim()) : [];
53
+ const confirmYes = async label => { await settle(); click([...document.querySelectorAll("dialog button")].find(b => b.textContent.trim() == label)); await wait(); };
54
+
55
+ check("the members: each with their email and role, the signed-in one marked (you), and the invitations waiting", rows().length == 3 && rows()[0].includes("(you)") && rows()[2].includes("esi@example.test") && (text("#invitations-title") ?? "").includes("Invitations waiting") && document.body.textContent.includes("yaw@example.test"));
56
+
57
+ const role = root.querySelector('select[aria-label="Role for Esi Mensa"]');
58
+ role.value = "admin"; role.dispatchEvent(new w.Event("change", { bubbles: true })); await wait();
59
+ check("a role changed: the API's asked, the list as it now is", api.calls.some(call => call.method == "PATCH" && call.path == "/api/organisations/1/members/3" && call.body.role == "admin") && /Esi Mensa Admin|Esi Mensa/.test(rows()[2]));
60
+
61
+ const remove = [...root.querySelectorAll("tbody tr")][2].querySelector(".link-button.danger");
62
+ click(remove); await confirmYes("Remove");
63
+ check("Remove asks first, then the API; they're gone from the list", api.calls.some(call => call.method == "DELETE" && call.path == "/api/organisations/1/members/3") && rows().length == 2);
64
+
65
+ click(button("Resend")); await wait();
66
+ check("Resend asks the API for that invitation", api.calls.some(call => call.method == "POST" && call.path == "/api/organisations/1/invitations/7/resend"));
67
+ click(button("Revoke")); await confirmYes("Revoke").catch(() => undefined);
68
+ check("Revoke takes the invitation back", api.calls.some(call => call.method == "DELETE" && call.path == "/api/organisations/1/invitations/7"));
69
+
70
+ // Inviting.
71
+ await router.push("/members/invite"); await wait();
72
+ click(button("Send invitation")); await settle();
73
+ check("an empty invitation is refused before it's sent", /email/i.test(text("#invite-email-error") ?? "") && api.calls.every(call => !(call.method == "POST" && call.path == "/api/organisations/1/invitations")));
74
+ type("#invite-email", "new@example.test");
75
+ click(button("Send invitation")); await wait();
76
+ check("an email and a role: sent, and back on Members with it waiting", api.calls.some(call => call.method == "POST" && call.path == "/api/organisations/1/invitations" && call.body.email == "new@example.test" && call.body.role == "member") && w.location.pathname == "/members" && /new@example\.test/.test(document.body.textContent));
77
+
78
+ // Settings.
79
+ await router.push("/settings"); await wait();
80
+ check("the settings: the organisation's name", $("#organisation-name").value == "Acme");
81
+ type("#organisation-name", "Acme Ltd");
82
+ click(button("Save changes")); await wait();
83
+ check("saved: the API's asked and the switcher shows the new name at once", api.calls.some(call => call.method == "PATCH" && call.path == "/api/organisations/1/settings" && call.body.name == "Acme Ltd") && /Acme Ltd/.test(text(".switcher-button") ?? "") && /Saved\./.test(text(".saved") ?? ""));
84
+
85
+ // A member.
86
+ user = apiUser({ organisations: [{ ...acme, role: "member" }] });
87
+ await session.load();
88
+ await router.push("/members"); await wait();
89
+ check("a member who asks for Members is told they don't have access, and which role they have", w.location.pathname == "/forbidden" && /access/i.test(document.body.textContent) && /a member in Acme/.test(document.body.textContent));
@@ -0,0 +1,49 @@
1
+ // What the app has told the user: the bell in the frame shows how many are unread, links to the page that lists them newest first, and the page marks one read or all, and deletes.
2
+ import { check } from "./support/check.mjs";
3
+ import { settle } from "./support/settle.mjs";
4
+ import { useDom } from "./support/dom.mjs";
5
+ import { useFakeApi } from "./support/api.mjs";
6
+ import { apiUser } from "./support/users.mjs";
7
+
8
+ const w = useDom('<main id="app"></main>', { url: "http://app.test/notifications" });
9
+ const api = useFakeApi();
10
+ let list = [
11
+ { id: "a1", type: "InviteeJoined", data: { message: "Yaw Asante accepted your invitation and joined Acme as a member." }, readAt: null, createdAt: "2026-11-02T10:00:00Z" },
12
+ { id: "a2", type: "RemovedFromOrganisation", data: { message: "Ama removed you from Bakery." }, readAt: "2026-11-01T09:00:00Z", createdAt: "2026-11-01T08:00:00Z" },
13
+ ];
14
+ const page = () => ({ notifications: list, total: list.length, unread: list.filter(one => one.readAt === null).length, page: 1, page_size: 25 });
15
+
16
+ api.get("/api/user", () => ({ json: apiUser() }));
17
+ api.get("/api/user/notifications", () => ({ json: page() }));
18
+ api.post("/api/user/notifications/a1/read", () => (list = list.map(one => one.id == "a1" ? { ...one, readAt: "2026-11-02T11:00:00Z" } : one), { status: 204 }));
19
+ api.post("/api/user/notifications/read", () => (list = list.map(one => ({ ...one, readAt: one.readAt ?? "x" })), { status: 204 }));
20
+ api.delete("/api/user/notifications/a2", () => (list = list.filter(one => one.id != "a2"), { status: 204 }));
21
+
22
+ const { createApp, createEvents, createStores } = await import("@mana-js/mana");
23
+ const { default: App } = await import("@/App.mana");
24
+ const { default: router } = await import("@/router");
25
+ const { useSession } = await import("@/stores/session");
26
+ let session;
27
+ const Probe = { setup() { session = useSession(); return {}; }, template: `<i></i>` };
28
+
29
+ createApp({ components: { App, Probe }, template: `<div><App /><Probe /></div>` }).use(createStores()).use(createEvents()).use(router).mount("#app");
30
+ await session.load();
31
+ await router.isReady();
32
+ await router.push("/notifications");
33
+ await new Promise(resolve => setTimeout(resolve, 50));
34
+ await settle();
35
+
36
+ const root = document.getElementById("app");
37
+ const $ = s => root.querySelector(s);
38
+ const text = s => $(s)?.textContent.replace(/\s+/g, " ").trim() ?? null;
39
+ const type = (id, value) => { const input = $(id); input.value = value; input.dispatchEvent(new w.Event("input")); };
40
+ const click = el => el.dispatchEvent(new w.MouseEvent("click", { bubbles: true, cancelable: true }));
41
+ const button = label => [...root.querySelectorAll("button")].find(b => b.textContent.trim() == label);
42
+ const wait = async () => { await new Promise(resolve => setTimeout(resolve, 30)); await settle(); };
43
+
44
+ check("the bell: one unread, and a link to the page", $(".bell .badge")?.textContent.trim() == "1" && /unread|notification/i.test($(".bell").getAttribute("aria-label") ?? ""));
45
+ check("the page lists them, newest first, what each says, the unread marked", /Notifications/.test(text("h1") ?? "") && [...root.querySelectorAll(".notices li")].length == 2 && /Yaw Asante accepted/.test(text(".notices li:first-child") ?? "") && root.querySelector(".notices li.unread"));
46
+ click([...root.querySelectorAll(".notices li button")].find(b => /Mark as read/.test(b.getAttribute("aria-label") ?? ""))); await wait();
47
+ check("Mark as read: the API's asked, nothing's unread, the bell's count goes", api.calls.some(call => call.path == "/api/user/notifications/a1/read") && !root.querySelector(".notices li.unread") && !$(".bell .badge"));
48
+ click([...root.querySelectorAll(".notices li button")].find(b => /Delete: Ama removed/.test(b.getAttribute("aria-label") ?? ""))); await wait();
49
+ check("Delete takes it away", api.calls.some(call => call.method == "DELETE" && call.path == "/api/user/notifications/a2") && root.querySelectorAll(".notices li").length == 1);