-
Notifications
You must be signed in to change notification settings - Fork 122
/
utils.ts
169 lines (148 loc) 路 5.12 KB
/
utils.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { it, expect } from "vitest";
import { Storage, Driver, createStorage, restoreSnapshot } from "../../src";
export interface TestContext {
storage: Storage;
driver: Driver;
}
export interface TestOptions {
driver: Driver;
additionalTests?: (ctx: TestContext) => void;
}
export function testDriver(opts: TestOptions) {
const ctx: TestContext = {
storage: createStorage({ driver: opts.driver }),
driver: opts.driver,
};
it("init", async () => {
await restoreSnapshot(ctx.storage, { initial: "works" });
expect(await ctx.storage.getItem("initial")).toBe("works");
await ctx.storage.clear();
});
it("initial state", async () => {
expect(await ctx.storage.hasItem("s1:a")).toBe(false);
expect(await ctx.storage.getItem("s2:a")).toBe(null);
expect(await ctx.storage.getKeys()).toMatchObject([]);
});
it("setItem", async () => {
await ctx.storage.setItem("s1:a", "test_data");
await ctx.storage.setItem("s2:a", "test_data");
await ctx.storage.setItem("s3:a?q=1", "test_data");
expect(await ctx.storage.hasItem("s1:a")).toBe(true);
expect(await ctx.storage.getItem("s1:a")).toBe("test_data");
expect(await ctx.storage.getItem("s3:a?q=2")).toBe("test_data");
});
it("getKeys", async () => {
expect(await ctx.storage.getKeys().then((k) => k.sort())).toMatchObject(
["s1:a", "s2:a", "s3:a"].sort()
);
expect(await ctx.storage.getKeys("s1").then((k) => k.sort())).toMatchObject(
["s1:a"].sort()
);
});
it("serialize (object)", async () => {
await ctx.storage.setItem("/data/test.json", { json: "works" });
expect(await ctx.storage.getItem("/data/test.json")).toMatchObject({
json: "works",
});
});
it("serialize (primitive)", async () => {
await ctx.storage.setItem("/data/true.json", true);
expect(await ctx.storage.getItem("/data/true.json")).toBe(true);
});
it("serialize (lossy object with toJSON())", async () => {
class Test1 {
toJSON() {
return "SERIALIZED";
}
}
await ctx.storage.setItem("/data/serialized1.json", new Test1());
expect(await ctx.storage.getItem("/data/serialized1.json")).toBe(
"SERIALIZED"
);
class Test2 {
toJSON() {
return { serializedObj: "works" };
}
}
await ctx.storage.setItem("/data/serialized2.json", new Test2());
expect(await ctx.storage.getItem("/data/serialized2.json")).toMatchObject({
serializedObj: "works",
});
});
it("serialize (error for non primitives)", async () => {
class Test {}
expect(
ctx.storage.setItem("/data/badvalue.json", new Test())
).rejects.toThrow("[unstorage] Cannot stringify value!");
});
it("raw support", async () => {
const value = new Uint8Array([1, 2, 3]);
await ctx.storage.setItemRaw("/data/raw.bin", value);
const rValue = await ctx.storage.getItemRaw("/data/raw.bin");
const rValueLen = rValue?.length || rValue?.byteLength;
if (rValueLen !== value.length) {
console.log("Invalid raw value length:", rValue, "Length:", rValueLen);
}
expect(rValueLen).toBe(value.length);
expect(Buffer.from(rValue).toString("base64")).toBe(
Buffer.from(value).toString("base64")
);
});
// Bulk tests
it("setItems", async () => {
await ctx.storage.setItems([
{ key: "t:1", value: "test_data_t1" },
{ key: "t:2", value: "test_data_t2" },
{ key: "t:3", value: "test_data_t3" },
]);
expect(await ctx.storage.getItem("t:1")).toBe("test_data_t1");
expect(await ctx.storage.getItem("t:2")).toBe("test_data_t2");
expect(await ctx.storage.getItem("t:3")).toBe("test_data_t3");
});
it("getItems", async () => {
await ctx.storage.setItem("v1:a", "test_data_v1:a");
await ctx.storage.setItem("v2:a", "test_data_v2:a");
await ctx.storage.setItem("v3:a?q=1", "test_data_v3:a?q=1");
expect(
await ctx.storage.getItems([{ key: "v1:a" }, "v2:a", { key: "v3:a?q=1" }])
).toMatchObject([
{
key: "v1:a",
value: "test_data_v1:a",
},
{
key: "v2:a",
value: "test_data_v2:a",
},
{
key: "v3:a", // key should lose the querystring
value: "test_data_v3:a?q=1",
},
]);
});
it("getItem - return falsy values when set in storage", async () => {
await ctx.storage.setItem("zero", 0);
expect(await ctx.storage.getItem("zero")).toBe(0);
await ctx.storage.setItem("my-false-flag", false);
expect(await ctx.storage.getItem("my-false-flag")).toBe(false);
});
// TODO: Refactor to move after cleanup
if (opts.additionalTests) {
opts.additionalTests(ctx);
}
it("removeItem", async () => {
await ctx.storage.removeItem("s1:a", false);
expect(await ctx.storage.hasItem("s1:a")).toBe(false);
expect(await ctx.storage.getItem("s1:a")).toBe(null);
});
it("clear", async () => {
await ctx.storage.clear();
expect(await ctx.storage.getKeys()).toMatchObject([]);
// ensure we can clear empty storage as well: #162
await ctx.storage.clear();
expect(await ctx.storage.getKeys()).toMatchObject([]);
});
it("dispose", async () => {
await ctx.storage.dispose();
});
}