2021-06-22 16:39:44 +00:00
|
|
|
import { KeyValueStorage, MemoryStorage } from "./basic.ts";
|
|
|
|
import { startRestServer } from "./server.ts";
|
2021-08-16 16:15:01 +00:00
|
|
|
import { describe, expect, getAvailablePort, it } from "./deps.testing.ts";
|
|
|
|
|
|
|
|
export { describe, expect, it };
|
2021-06-21 22:05:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic high-level test suite for any kind storage
|
|
|
|
*/
|
|
|
|
export async function basicCheck(storage: KeyValueStorage): Promise<void> {
|
|
|
|
expect(await storage.get("test")).toBe(null);
|
|
|
|
await storage.set("test", "val");
|
|
|
|
expect(await storage.get("test")).toBe("val");
|
|
|
|
expect(await storage.get("notest")).toBe(null);
|
|
|
|
await storage.set("test", null);
|
|
|
|
expect(await storage.get("test")).toBe(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Temporarily disable localStorage (constructor will throw error),
|
|
|
|
* for the duration of the callback
|
|
|
|
*/
|
|
|
|
export async function disableLocalStorage(
|
|
|
|
body: () => Promise<void>,
|
|
|
|
): Promise<void> {
|
|
|
|
const ns = window as any;
|
|
|
|
const removed = ns["localStorage"];
|
|
|
|
delete ns["localStorage"];
|
|
|
|
try {
|
2021-06-22 20:07:26 +00:00
|
|
|
await body();
|
2021-06-21 22:05:08 +00:00
|
|
|
} finally {
|
|
|
|
ns["localStorage"] = removed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a temporary test server, with memory storage
|
|
|
|
*/
|
|
|
|
export async function runTestServer(
|
|
|
|
body: (url: string) => Promise<void>,
|
|
|
|
): Promise<void> {
|
2021-06-22 16:39:44 +00:00
|
|
|
const port = await getAvailablePort({ port: { start: 3000, end: 30000 } });
|
2021-06-21 22:05:08 +00:00
|
|
|
if (!port) {
|
|
|
|
throw new Error("No port available for test server");
|
|
|
|
}
|
|
|
|
|
|
|
|
const app = await startRestServer(port, new MemoryStorage());
|
|
|
|
try {
|
|
|
|
await body(`http://localhost:${port}`);
|
|
|
|
} finally {
|
|
|
|
await app.close();
|
|
|
|
}
|
|
|
|
}
|