-
Notifications
You must be signed in to change notification settings - Fork 20
/
socialApp.test.ts
153 lines (132 loc) · 4.04 KB
/
socialApp.test.ts
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
import { strict as assert } from "assert"
import { describe, it } from "mocha"
import { transactionalReadWrite } from "../database/sync/transactionalReadWrite"
import { TupleDatabase } from "../database/sync/TupleDatabase"
import { TupleDatabaseClient } from "../database/sync/TupleDatabaseClient"
import { namedTupleToObject } from "../helpers/namedTupleToObject"
import { ReadOnlyTupleDatabaseClientApi } from "../main"
import { InMemoryTupleStorage } from "../storage/InMemoryTupleStorage"
type User = { username: string; bio: string }
type Post = {
id: string
username: string
timestamp: number
text: string
}
type Schema =
| { key: ["user", { username: string }]; value: User }
| { key: ["post", { id: string }]; value: Post }
| {
key: ["follows", { from: string }, { to: string }]
value: null
}
| {
key: ["following", { to: string }, { from: string }]
value: null
}
| {
key: [
"profile",
{ username: string },
{ timestamp: number },
{ postId: string }
]
value: null
}
| {
key: [
"feed",
{ username: string },
{ timestamp: number },
{ postId: string }
]
value: null
}
const addFollow = transactionalReadWrite<Schema>()(
(tx, from: string, to: string) => {
// Setup the follow relationships.
tx.set(["follows", { from }, { to }], null)
tx.set(["following", { to }, { from }], null)
// Get the followed user's posts.
tx.scan({ prefix: ["profile", { username: to }] })
.map(({ key }) => namedTupleToObject(key))
.forEach(({ timestamp, postId }) => {
// Write those posts to the user's feed.
tx.set(["feed", { username: from }, { timestamp }, { postId }], null)
})
}
)
const createPost = transactionalReadWrite<Schema>()((tx, post: Post) => {
tx.set(["post", { id: post.id }], post)
// Add to the user's profile
const { username, timestamp } = post
tx.set(["profile", { username }, { timestamp }, { postId: post.id }], null)
// Find everyone who follows this username.
const followers = tx
.scan({ prefix: ["following", { to: username }] })
.map(({ key }) => namedTupleToObject(key))
.map(({ from }) => from)
// Write to their feed.
followers.forEach((username) => {
tx.set(["feed", { username }, { timestamp }, { postId: post.id }], null)
})
})
const createUser = transactionalReadWrite<Schema>()((tx, user: User) => {
tx.set(["user", { username: user.username }], user)
})
function getFeed(db: ReadOnlyTupleDatabaseClientApi<Schema>, username: string) {
return db
.scan({ prefix: ["feed", { username }] })
.map(({ key }) => namedTupleToObject(key))
.map(({ postId }) => postId)
}
function getProfile(
db: ReadOnlyTupleDatabaseClientApi<Schema>,
username: string
) {
return db
.scan({ prefix: ["profile", { username }] })
.map(({ key }) => namedTupleToObject(key))
.map(({ postId }) => postId)
}
describe("Social App", () => {
it("works", () => {
// Lets try it out.
const db = new TupleDatabaseClient<Schema>(
new TupleDatabase(new InMemoryTupleStorage())
)
createUser(db, { username: "chet", bio: "I like to build things." })
createUser(db, { username: "elon", bio: "Let's go to mars." })
createUser(db, { username: "meghan", bio: "" })
// Chet makes a post.
createPost(db, {
id: "post1",
username: "chet",
timestamp: 1,
text: "post1",
})
createPost(db, {
id: "post2",
username: "meghan",
timestamp: 2,
text: "post2",
})
assert.deepEqual(getProfile(db, "chet"), ["post1"])
assert.deepEqual(getProfile(db, "meghan"), ["post2"])
assert.deepEqual(getFeed(db, "chet"), [])
assert.deepEqual(getFeed(db, "meghan"), [])
// When meghan follows chet, the post should appear in her feed.
addFollow(db, "meghan", "chet")
assert.deepEqual(getFeed(db, "chet"), [])
assert.deepEqual(getFeed(db, "meghan"), ["post1"])
// When chet makes another post, it should show up in meghan's feed.
createPost(db, {
id: "post3",
username: "chet",
timestamp: 3,
text: "post3",
})
assert.deepEqual(getProfile(db, "chet"), ["post1", "post3"])
assert.deepEqual(getFeed(db, "meghan"), ["post1", "post3"])
})
})