forked from unjs/unstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.test.ts
225 lines (193 loc) · 6.48 KB
/
storage.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
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
217
218
219
220
221
222
223
224
225
import { describe, it, expect, vi } from "vitest";
import {
createStorage,
snapshot,
restoreSnapshot,
prefixStorage,
} from "../src";
import memory from "../src/drivers/memory";
const data = {
"etc:conf": "test",
"data:foo": 123,
};
describe("storage", () => {
it("mount/unmount", async () => {
const storage = createStorage().mount("/mnt", memory());
await restoreSnapshot(storage, data, "mnt");
expect(await snapshot(storage, "/mnt")).toMatchObject(data);
});
it("getMount and getMounts", () => {
const storage = createStorage();
storage.mount("/mnt", memory());
storage.mount("cache", memory());
storage.mount("cache:sub", memory());
expect(storage.getMount("/cache:").base).toBe("cache:");
expect(storage.getMount("/cache:foo").base).toBe("cache:");
expect(storage.getMount("/cache:sub").base).toBe("cache:sub:");
expect(storage.getMount("/cache:sub:foo").base).toBe("cache:sub:");
expect(storage.getMounts("/cache").map((m) => m.base))
.toMatchInlineSnapshot(`
[
"cache:sub:",
"cache:",
]
`);
expect(storage.getMounts("/cache:sub").map((m) => m.base))
.toMatchInlineSnapshot(`
[
"cache:sub:",
]
`);
expect(
storage.getMounts("/cache:sub", { parents: true }).map((m) => m.base)
).toMatchInlineSnapshot(`
[
"cache:sub:",
"cache:",
"",
]
`);
expect(storage.getMounts().map((m) => m.base)).toMatchInlineSnapshot(`
[
"cache:sub:",
"cache:",
"mnt:",
"",
]
`);
});
it("snapshot", async () => {
const storage = createStorage();
await restoreSnapshot(storage, data);
expect(await snapshot(storage, "")).toMatchObject(data);
});
it("watch", async () => {
const onChange = vi.fn();
const storage = createStorage().mount("/mnt", memory());
await storage.watch(onChange);
await restoreSnapshot(storage, data, "mnt");
expect(onChange).toHaveBeenCalledWith("update", "mnt:etc:conf");
expect(onChange).toHaveBeenCalledWith("update", "mnt:data:foo");
expect(onChange).toHaveBeenCalledTimes(2);
});
it("unwatch return", async () => {
const onChange = vi.fn();
const storage = createStorage().mount("/mnt", memory());
const unwatch = await storage.watch(onChange);
await storage.setItem("mnt:data:foo", 42);
await unwatch();
await storage.setItem("mnt:data:foo", 41);
expect(onChange).toHaveBeenCalledTimes(1);
});
it("unwatch all", async () => {
const onChange = vi.fn();
const storage = createStorage().mount("/mnt", memory());
await storage.watch(onChange);
await storage.setItem("mnt:data:foo", 42);
await storage.unwatch();
await storage.setItem("mnt:data:foo", 41);
expect(onChange).toHaveBeenCalledTimes(1);
});
it("mount overides", async () => {
const mainStorage = memory();
const storage = createStorage({ driver: mainStorage });
await storage.setItem("/mnt/test.txt", "v1");
await storage.setItem("/mnt/test.base.txt", "v1");
const initialKeys = await storage.getKeys();
expect(initialKeys).toMatchInlineSnapshot(`
[
"mnt:test.txt",
"mnt:test.base.txt",
]
`);
storage.mount("/mnt", memory());
await storage.setItem("/mnt/test.txt", "v2");
await storage.setItem("/mnt/foo/test.txt", "v3");
storage.mount("/mnt/foo", memory());
expect(await storage.getItem("/mnt/foo/test.txt")).toBe(null);
expect(await storage.getItem("/mnt/test.txt")).toBe("v2");
expect(await storage.getKeys()).toMatchInlineSnapshot(`
[
"mnt:test.txt",
]
`);
await storage.clear("/mnt");
await storage.unmount("/mnt");
expect(await storage.getKeys()).toMatchObject(initialKeys);
expect(await storage.getItem("/mnt/test.txt")).toBe("v1");
});
});
describe("utils", () => {
it("prefixStorage", async () => {
const storage = createStorage();
const pStorage = prefixStorage(storage, "foo");
await pStorage.setItem("x", "bar");
await pStorage.setItem("y", "baz");
expect(await storage.getItem("foo:x")).toBe("bar");
expect(await pStorage.getItem("x")).toBe("bar");
expect(await pStorage.getKeys()).toStrictEqual(["x", "y"]);
// Higher order storage
const secondStorage = createStorage();
secondStorage.mount("/mnt", storage);
const mntStorage = prefixStorage(secondStorage, "mnt");
expect(await mntStorage.getKeys()).toStrictEqual(["foo:x", "foo:y"]);
// Get keys from sub-storage
expect(await mntStorage.getKeys("foo")).toStrictEqual(["foo:x", "foo:y"]);
});
it("stringify", () => {
const storage = createStorage();
expect(async () => await storage.setItem("foo", [])).not.toThrow();
});
it("has aliases", async () => {
const storage = createStorage();
await storage.setItem("foo", "bar");
expect(await storage.has("foo")).toBe(true);
expect(await storage.get("foo")).toBe("bar");
expect(await storage.keys()).toEqual(["foo"]);
await storage.del("foo");
expect(await storage.has("foo")).toBe(false);
await storage.setItem("bar", "baz");
await storage.remove("bar");
expect(await storage.has("bar")).toBe(false);
});
});
describe("Regression", () => {
it("setItems doeesn't upload twice", async () => {
/**
* https://github.com/unjs/unstorage/pull/392
*/
const setItem = vi.fn();
const setItems = vi.fn();
const driver = memory();
const storage = createStorage({
driver: {
...driver,
setItem: (...args) => {
setItem(...args);
return driver.setItem?.(...args);
},
setItems: (...args) => {
setItems(...args);
return driver.setItems?.(...args);
},
},
});
await storage.setItems([{ key: "foo.txt", value: "bar" }]);
expect(setItem).toHaveBeenCalledTimes(0);
expect(setItems).toHaveBeenCalledTimes(1);
});
it("prefixed storage supports aliases", async () => {
const storage = createStorage();
const pStorage = prefixStorage(storage, "foo");
await pStorage.set("x", "foo");
await pStorage.set("y", "bar");
expect(await pStorage.get("x")).toBe("foo");
expect(await pStorage.get("x")).toBe("foo");
expect(await pStorage.has("x")).toBe(true);
expect(await pStorage.get("y")).toBe("bar");
await pStorage.del("x");
expect(await pStorage.has("x")).toBe(false);
await pStorage.remove("y");
expect(await pStorage.has("y")).toBe(false);
});
});