-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathget_update_info.spec.ts
74 lines (64 loc) · 1.96 KB
/
get_update_info.spec.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
import { PGlite } from "@electric-sql/pglite";
import type { Bundle, GetBundlesArgs, UpdateInfo } from "@hot-updater/core";
import { setupGetUpdateInfoTestSuite } from "@hot-updater/core/test-utils";
import camelcaseKeys from "camelcase-keys";
import { afterAll, beforeEach, describe } from "vitest";
import { prepareSql } from "./prepareSql";
const createInsertBundleQuery = (bundle: Bundle) => {
return `
INSERT INTO bundles (
id, file_url, file_hash, platform, target_app_version,
should_force_update, enabled, git_commit_hash, message
) VALUES (
'${bundle.id}',
'${bundle.fileUrl}',
'${bundle.fileHash}',
'${bundle.platform}',
'${bundle.targetAppVersion}',
${bundle.shouldForceUpdate},
${bundle.enabled},
${bundle.gitCommitHash ? `'${bundle.gitCommitHash}'` : "null"},
${bundle.message ? `'${bundle.message}'` : "null"}
);
`;
};
const createGetUpdateInfo =
(db: PGlite) =>
async (
bundles: Bundle[],
{ appVersion, bundleId, platform }: GetBundlesArgs,
): Promise<UpdateInfo | null> => {
await db.exec(createInsertBundleQuerys(bundles));
const result = await db.query<{
id: string;
should_force_update: boolean;
file_url: string;
file_hash: string;
status: string;
}>(
`
SELECT * FROM get_update_info('${platform}', '${appVersion}', '${bundleId}')
`,
);
return result.rows[0]
? (camelcaseKeys(result.rows[0]) as UpdateInfo)
: null;
};
const createInsertBundleQuerys = (bundles: Bundle[]) => {
return bundles.map(createInsertBundleQuery).join("\n");
};
const db = new PGlite();
const sql = await prepareSql();
await db.exec(sql);
const getUpdateInfo = createGetUpdateInfo(db);
describe("getUpdateInfo", () => {
beforeEach(async () => {
await db.exec("DELETE FROM bundles");
});
afterAll(async () => {
await db.close();
});
setupGetUpdateInfoTestSuite({
getUpdateInfo: getUpdateInfo,
});
});