Skip to content
Myr

accounts.test.ts

This file

Path

apps/platform/test/accounts.test.ts

234 lines · 7.4 KiB · TypeScript

Last changed by

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–8why, one step back
1import { NodeServices } from "@effect/platform-node";
2import { SqliteClient } from "@effect/sql-sqlite-node";3import { describe, expect, it } from "@effect/vitest";4import { Handle } from "@repo/protocol/ids";5import { Effect, Exit, FileSystem, Layer, Option } from "effect";6import { SqlClient } from "effect/sql/SqlClient";78import { AccountsD1 } from "../src/adapters/accounts-d1.ts";

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

lines 9–15why, one step back
9import {
10 Accounts,11 DisplayName,12 Email,13 EmailTaken,14 HandleTaken,15} from "../src/auth/accounts.ts";

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 16–24why, one step back
16import { AccountsMemory } from "../src/auth/accounts/memory.ts";
17import { PasswordHash } from "../src/auth/password.ts";1819const alice = Handle.make("alice");2021const first = PasswordHash.make("pbkdf2-sha256$100000$Zmlyc3Q$Zmlyc3Q");2223const second = PasswordHash.make("pbkdf2-sha256$100000$c2Vjb25k$c2Vjb25k");24

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

lines 25–28why, one step back
25const aliceEmail = Email.make("alice@mail.example");
2627const aliceName = DisplayName.make("Alice Example");28

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 29–37why, one step back
29const find = (handle: string) =>
30 Accounts.use((accounts) => accounts.find(Handle.make(handle)));3132const describeAccounts = (adapter: string, layer: Layer.Layer<Accounts>) => {33 describe(`Accounts over ${adapter}`, () => {34 it.effect("finds an account by its handle, in any case", () =>35 Effect.gen(function* () {36 const accounts = yield* Accounts;37

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

line 38why, one step back
38 yield* accounts.create({ handle: Handle.make("Alice"), hash: first });

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 39–62why, one step back
39
40 const found = Option.some({41 handle: Handle.make("Alice"),42 hash: first,43 });4445 expect(yield* find("Alice")).toStrictEqual(found);46 expect(yield* find("alice")).toStrictEqual(found);47 expect(yield* find("ALICE")).toStrictEqual(found);48 }).pipe(Effect.provide(layer))49 );5051 it.effect("finds nobody under a handle no one signed up with", () =>52 Effect.gen(function* () {53 expect(yield* find("bob")).toStrictEqual(Option.none());54 }).pipe(Effect.provide(layer))55 );5657 it.effect(58 "refuses a second account whose handle differs only in case, and keeps the first",59 () =>60 Effect.gen(function* () {61 const accounts = yield* Accounts;62

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

line 63why, one step back
63 yield* accounts.create({ handle: alice, hash: first });

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 64–65why, one step back
64
65 const taken = yield* Effect.flip(

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

line 66why, one step back
66 accounts.create({ handle: Handle.make("ALICE"), hash: second })

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 67–83why, one step back
67 );
6869 expect(taken).toStrictEqual(70 new HandleTaken({ handle: Handle.make("ALICE") })71 );72 expect(yield* find("alice")).toStrictEqual(73 Option.some({ handle: alice, hash: first })74 );75 }).pipe(Effect.provide(layer))76 );7778 it.effect("lets exactly one of two racing sign-ups have a handle", () =>79 Effect.gen(function* () {80 const accounts = yield* Accounts;8182 const exits = yield* Effect.all(83 [

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

lines 84–87why, one step back
84 Effect.exit(accounts.create({ handle: alice, hash: first })),
85 Effect.exit(86 accounts.create({ handle: Handle.make("Alice"), hash: second })87 ),

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 88–99why, one step back
88 ],
89 { concurrency: "unbounded" }90 );9192 expect(exits.filter(Exit.isSuccess)).toHaveLength(1);93 }).pipe(Effect.provide(layer))94 );9596 it.effect("replaces an account's password hash", () =>97 Effect.gen(function* () {98 const accounts = yield* Accounts;99

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

line 100why, one step back
100 yield* accounts.create({ handle: alice, hash: first });

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 101–107why, one step back
101 yield* accounts.setPasswordHash(Handle.make("ALICE"), second);
102103 expect(yield* find("alice")).toStrictEqual(104 Option.some({ handle: alice, hash: second })105 );106 }).pipe(Effect.provide(layer))107 );

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

lines 108–197why, one step back
108
109 it.effect(110 "keeps an account's email and display name, and finds it by its email in any case",111 () =>112 Effect.gen(function* () {113 const accounts = yield* Accounts;114115 const account = {116 displayName: aliceName,117 email: aliceEmail,118 handle: alice,119 hash: first,120 };121122 yield* accounts.create(account);123124 expect(yield* find("ALICE")).toStrictEqual(Option.some(account));125 expect(126 yield* accounts.findByEmail(Email.make("ALICE@Mail.Example"))127 ).toStrictEqual(Option.some(account));128 expect(129 yield* accounts.findByEmail(Email.make("bob@mail.example"))130 ).toStrictEqual(Option.none());131 }).pipe(Effect.provide(layer))132 );133134 it.effect(135 "refuses a second account with the same email in another case, and lets any number have none",136 () =>137 Effect.gen(function* () {138 const accounts = yield* Accounts;139140 yield* accounts.create({141 email: aliceEmail,142 handle: alice,143 hash: first,144 });145 yield* accounts.create({ handle: Handle.make("carol"), hash: first });146 yield* accounts.create({ handle: Handle.make("dave"), hash: first });147148 const shouted = Email.make("ALICE@mail.example");149150 expect(151 yield* Effect.flip(152 accounts.create({153 email: shouted,154 handle: Handle.make("bob"),155 hash: second,156 })157 )158 ).toStrictEqual(new EmailTaken({ email: shouted }));159 expect(yield* find("bob")).toStrictEqual(Option.none());160 }).pipe(Effect.provide(layer))161 );162163 it.effect(164 "sets an account's email, unless another account holds it or no account has the handle",165 () =>166 Effect.gen(function* () {167 const accounts = yield* Accounts;168169 const bobEmail = Email.make("bob@mail.example");170171 yield* accounts.create({ handle: alice, hash: first });172 yield* accounts.create({173 email: bobEmail,174 handle: Handle.make("bob"),175 hash: second,176 });177178 yield* accounts.setEmail(Handle.make("ALICE"), aliceEmail);179180 expect(yield* find("alice")).toStrictEqual(181 Option.some({ email: aliceEmail, handle: alice, hash: first })182 );183 expect(184 yield* Effect.flip(accounts.setEmail(alice, bobEmail))185 ).toStrictEqual(new EmailTaken({ email: bobEmail }));186 expect(187 yield* Effect.exit(188 accounts.setEmail(189 Handle.make("nobody"),190 Email.make("n@mail.example")191 )192 )193 ).toStrictEqual(194 Exit.fail(new EmailTaken({ email: Email.make("n@mail.example") }))195 );196 }).pipe(Effect.provide(layer))197 );

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 198–208why, one step back
198 });
199};200201describeAccounts("memory", Layer.fresh(AccountsMemory.layer));202203const migrated = Layer.effectDiscard(204 Effect.gen(function* () {205 const fs = yield* FileSystem.FileSystem;206207 const sql = yield* SqlClient;208

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

lines 209–212why, one step back
209 for (const migration of ["0002_accounts.sql", "0006_account_email.sql"]) {
210 const ddl = yield* fs.readFileString(211 new URL(`../migrations/${migration}`, import.meta.url).pathname212 );

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

line 213why, one step back
213

Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c

lines 214–225why, one step back
214 // One statement a call; a comment may hold a semicolon, so comments go first.
215 const statements = ddl216 .split("\n")217 .filter((line) => !line.startsWith("--"))218 .join("\n")219 .split(";")220 .filter((part) => part.trim() !== "");221222 for (const statement of statements) {223 yield* sql.unsafe(statement);224 }225 }

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 226–234why, one step back
226 })
227).pipe(Layer.orDie, Layer.provide(NodeServices.layer));228229const database = migrated.pipe(230 Layer.provideMerge(SqliteClient.layer({ filename: ":memory:" })),231 Layer.orDie232);233234describeAccounts("D1 (sqlite-node)", AccountsD1.pipe(Layer.provide(database)));