Repository navigation
Expand file tree
/
Copy pathdomainGuide.test.ts
More file actions
216 lines (199 loc) · 6.57 KB
/
Copy pathdomainGuide.test.ts
File metadata and controls
216 lines (199 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// @vitest-environment node
/** `docs/entity.md`'s walk through one domain, run: what the guide says each reader answers. */
import { Optic, Schema } from 'effect'
import {
Derived,
Entity,
Expr,
Laws,
Order,
Query as Body,
Relation,
Write,
evaluate,
} from 'foldkit-entity'
import { Guards } from 'foldkit-guard'
import { Can, Mutation, Query, Remote } from 'foldkit-remote'
import { ModelRef } from 'foldkit-surface'
import { describe, expect, it } from 'vitest'
const User = Schema.Struct({ id: Schema.String })
const ProductFields = Entity.define(
'Product',
Schema.Struct({
id: Schema.String,
ownerId: Schema.String,
name: Schema.String.check(Schema.isMinLength(1)),
status: Schema.Literals(['draft', 'published']),
stock: Schema.Number,
reserved: Schema.Number,
revision: Schema.Number,
}),
)
const ReviewFields = Entity.define(
'Review',
Schema.Struct({ id: Schema.String, stars: Schema.Number }),
)
const Catalog = Entity.relate(
{ Product: ProductFields, Review: ReviewFields },
{ Review: { product: Relation.one(ProductFields) } },
)
const Shop = Guards.declare(
'Shop',
User,
guard => ({
isOwner: guard.row(ProductFields, user => Expr.eq(ProductFields.fields.ownerId, user.id)),
}),
{ key: user => user.id, unguarded: 'allow' },
)
const Product = Catalog.Product.pipe(
Entity.derived({ reviewCount: Derived.count(Catalog.Review.relations.product) }),
Entity.invariant('reservedWithinStock', ({ after }) => Expr.lte(after.reserved, after.stock), {
message: 'Reserved stock cannot exceed the stock',
path: ['reserved'],
}),
Guards.attach(Shop, { row: { Read: [Shop.isOwner], Update: [Shop.isOwner] } }),
)
const Draft = Entity.refine(Product, 'Draft', { status: 'draft' })
const Published = Entity.refine(Product, 'Published', { status: 'published' })
const Publish = Mutation.write(
'Publish',
Write.update(
Entity.input(
Product,
Schema.Struct({ id: Schema.String, status: Schema.Literal('published') }),
),
{ id: 'id', from: Draft, becomes: Published },
),
)
const Reserve = Mutation.write(
'Reserve',
Write.update(
Entity.input(Product, Schema.Struct({ id: Schema.String, reserved: Schema.Number })),
{ id: 'id' },
),
)
const ForSale = Query.define('ForSale', { search: Schema.String }, ({ input }) =>
Body.from(Published).pipe(
Body.where(Expr.contains(Product.fields.name, input.search)),
Body.orderBy(Order.desc(Product.fields.stock)),
),
)
const Card = Entity.select(Product, { name: true, stock: true, reviewCount: true })
const bolt = {
id: 'p1',
ownerId: 'ada',
name: 'Bolt',
status: 'draft',
stock: 10,
reserved: 2,
revision: 1,
}
const rows = [
bolt,
{
id: 'p2',
ownerId: 'ada',
name: 'Rivet',
status: 'published',
stock: 5,
reserved: 0,
revision: 1,
},
{
id: 'p3',
ownerId: 'bo',
name: 'Bolt cutter',
status: 'published',
stock: 7,
reserved: 7,
revision: 1,
},
]
describe('sixty seconds: one rule, three readers', () => {
const Item = Entity.define(
'Item',
Schema.Struct({ id: Schema.String, stock: Schema.Number, reserved: Schema.Number }),
).pipe(
Entity.invariant('reservedWithinStock', ({ after }) => Expr.lte(after.reserved, after.stock), {
message: 'Reserved stock cannot exceed the stock',
path: ['reserved'],
}),
)
const Hold = Mutation.write(
'Hold',
Write.update(
Entity.input(Item, Schema.Struct({ id: Schema.String, reserved: Schema.Number })),
{ id: 'id' },
),
)
it('the judge refuses the row, and the check says the write can break the rule', () => {
expect(Entity.judge(Item, { after: { id: 'i1', stock: 3, reserved: 5 } })._tag).toBe('Invalid')
expect(Entity.judge(Item, { after: { id: 'i1', stock: 5, reserved: 5 } })._tag).toBe('Valid')
expect(Laws.show(Laws.check({ writes: [Hold] }))).toBe(
[
'Hold x reservedWithinStock refused: Reserved stock cannot exceed the stock',
' before reserved: 0, stock: 0',
' input reserved: 1',
' after reserved: 1, stock: 0',
'',
'1 entity, 0 states, 1 write, 1 rule: 1 pair, 0 kept, 1 refused, 0 undecided',
].join('\n'),
)
})
})
describe('one domain declaration, read by several readers', () => {
it('the reference judge refuses a row that breaks the invariant, by its id and path', () => {
const verdict = Entity.judge(Product, { after: { ...bolt, reserved: 11 } })
expect(verdict._tag).toBe('Invalid')
expect(Entity.judge(Product, { after: bolt })._tag).toBe('Valid')
})
it('the question is one body: shown, and answered over rows in memory', () => {
const { body } = ForSale
expect(Body.show(body)).toBe(
[
'FROM Product',
'WHERE Product.Published',
' AND contains(Product.name, $search)',
'ORDER BY Product.stock DESC',
].join('\n'),
)
expect(evaluate(body, { search: 'bolt' }, rows).map(row => row.id)).toEqual(['p3'])
expect(evaluate(body, { search: '' }, rows).map(row => row.id)).toEqual(['p3', 'p2'])
})
it('the Selection carries a schema of what a card reads', () => {
expect(Object.keys(Card.schema.fields)).toEqual(['name', 'stock', 'reviewCount'])
})
it('Laws.check reads the writes and the states, with no database', () => {
expect(Laws.show(Laws.check({ writes: [Publish, Reserve] }))).toBe(
[
'Product',
' Draft -> Published Publish holds',
' Draft unreached',
' Published terminal',
'',
'Publish x reservedWithinStock kept: writes no member it reads',
'Reserve x reservedWithinStock refused: Reserved stock cannot exceed the stock',
' before reserved: 0, stock: 0',
' input reserved: 1',
' after reserved: 1, stock: 0',
'',
'1 entity, 2 states, 2 writes, 1 rule: 2 pairs, 1 kept, 1 refused, 0 undecided',
].join('\n'),
)
})
it('a page asks before sending, from what it holds', () => {
const Model = Schema.Struct({ remote: Remote.Model })
const Data = Remote.make({
model: ModelRef.fromOptic(Remote.Model, Optic.id<typeof Model.Type>().key('remote'), [
'remote',
]),
entities: [Product],
queries: [ForSale],
mutations: [Publish, Reserve],
})
const answer = Data.can({ remote: Remote.initial }, Publish, { id: 'p1', status: 'published' })
expect(
Can.match(answer, { Yes: () => 'yes', No: () => 'no', Undecided: () => 'undecided' }),
).toBe('undecided')
})
})