-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathhelpers.ts
More file actions
119 lines (111 loc) · 3.19 KB
/
helpers.ts
File metadata and controls
119 lines (111 loc) · 3.19 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
import {
ClientSecurity,
NewClient,
ZedClientInterface,
RelationshipUpdate_Operation,
} from '../v1.js'
/**
* Generates a random token to support unique, idempotent local testing.
*/
export function generateTestToken(): string {
return Math.random().toString()
}
/*
* Asserts that a value or expression is non-falsey.
* It's mostly useful to use a statement to narrow a type
* (as opposed to using an if statement, which is annoying
* in tests)
*/
export function assert(val: unknown, msg = "Assertion failed"): asserts val {
if(!val) throw new Error(msg);
}
export const testClient = () => NewClient(
generateTestToken(),
'localhost:50051',
ClientSecurity.INSECURE_LOCALHOST_ALLOWED
)
const testSchema = `
caveat likes_harry_potter(likes bool) {
likes == true
}
definition post {
relation writer: user
relation reader: user
relation caveated_reader: user with likes_harry_potter
permission write = writer
permission view = reader + writer
permission view_as_fan = caveated_reader + writer
}
definition user {}
`
export const writeTestSchema = async (client: ZedClientInterface) => {
await client.promises.writeSchema({
schema: testSchema
})
}
export const writeTestTuples = async (client: ZedClientInterface) => {
const emilia = {
object: {
objectType: "user",
objectId: "emilia"
},
optionalRelation: "",
}
const beatrice = {
object: {
objectType: "user",
objectId: "beatrice"
},
optionalRelation: "",
}
const postOne = {
objectType: "post",
objectId: "post-one"
}
const postTwo = {
objectType: "post",
objectId: "post-two"
}
await client.promises.writeRelationships({
// NOTE: optionalPreconditions seems like it should be omittable,
// but the way that the typescript plugin handles repeated fields
// is to treat them as required.
optionalPreconditions: [],
updates: [
{
operation: RelationshipUpdate_Operation.CREATE,
relationship: {
subject: emilia,
relation: "writer",
resource: postOne,
}
},
{
operation: RelationshipUpdate_Operation.CREATE,
relationship: {
subject: emilia,
relation: "writer",
resource: postTwo,
}
},
{
operation: RelationshipUpdate_Operation.CREATE,
relationship: {
subject: beatrice,
relation: "reader",
resource: postOne,
}
},
{
operation: RelationshipUpdate_Operation.CREATE,
relationship: {
subject: beatrice,
relation: "caveated_reader",
resource: postOne,
optionalCaveat: { caveatName: "likes_harry_potter" }
}
},
]
})
return { emilia, beatrice, postOne, postTwo };
}