|
1 | 1 | /// <reference types="jest-extended" /> |
2 | 2 |
|
3 | | -import { Asset, Connection, Environment, Key, createConnectionOptions } from '@map-colonies/auth-core'; |
| 3 | +import { Asset, Bundle, Connection, Environment, Key, createConnectionOptions } from '@map-colonies/auth-core'; |
4 | 4 | import { DataSource } from 'typeorm'; |
5 | 5 | import { ConnectionNotInitializedError } from '@src'; |
6 | 6 | import { BundleDatabase } from '@src/db'; |
| 7 | +import * as execa from '@src/wrappers/execa'; |
7 | 8 | import { getMockKeys } from './utils/key'; |
8 | 9 | import { getFakeAsset } from './utils/asset'; |
9 | 10 | import { getFakeConnection } from './utils/connection'; |
10 | 11 | import { getConfig, initConfig } from './helpers/config'; |
11 | 12 |
|
| 13 | +jest.mock('../src/wrappers/execa', () => { |
| 14 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-return |
| 15 | + return { |
| 16 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 17 | + __esModule: true, |
| 18 | + ...jest.requireActual('../src/wrappers/execa'), |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +type ExecaChildProcess = Awaited<ReturnType<(typeof execa)['execa']>>; |
| 23 | + |
12 | 24 | describe('db.ts', function () { |
13 | 25 | let dataSource: DataSource; |
14 | 26 | const asset = { ...getFakeAsset(), environment: [Environment.PRODUCTION], name: 'aviaviavi' }; |
@@ -57,45 +69,58 @@ describe('db.ts', function () { |
57 | 69 |
|
58 | 70 | describe('#saveBundle', function () { |
59 | 71 | it('should save the bundle to the db', async function () { |
| 72 | + const VERSION_OUTPUT = 'Version: 0.52.0\nBuild Commit: 8d2c137662560cac83d9cf24cbdaecc934910333\nBuild Timestamp: 2023-04-27T17:57:23Z'; |
| 73 | + jest.spyOn(execa, 'execa').mockResolvedValue({ stdout: VERSION_OUTPUT } as ExecaChildProcess); |
| 74 | + |
60 | 75 | const db = new BundleDatabase(dataSource); |
61 | 76 |
|
62 | 77 | const res = await db.saveBundle({ assets: [], connections: [], environment: Environment.PRODUCTION, keyVersion: 3 }, 'xdxd'); |
63 | 78 |
|
64 | 79 | expect(res).toBeGreaterThan(0); |
| 80 | + const bundle = await dataSource.getRepository(Bundle).findOneByOrFail({ id: res }); |
| 81 | + |
| 82 | + expect(bundle).toMatchObject({ |
| 83 | + environment: Environment.PRODUCTION, |
| 84 | + keyVersion: 3, |
| 85 | + opaVersion: '0.52.0', |
| 86 | + hash: 'xdxd', |
| 87 | + assets: [], |
| 88 | + connections: [], |
| 89 | + }); |
65 | 90 | }); |
66 | | - }); |
67 | 91 |
|
68 | | - describe('#getLatestVersions', function () { |
69 | | - it('should fetch the latest versions from the database', async function () { |
70 | | - const db = new BundleDatabase(dataSource); |
| 92 | + describe('#getLatestVersions', function () { |
| 93 | + it('should fetch the latest versions from the database', async function () { |
| 94 | + const db = new BundleDatabase(dataSource); |
71 | 95 |
|
72 | | - const { assets, connections, keyVersion } = await db.getLatestVersions(Environment.PRODUCTION); |
| 96 | + const { assets, connections, keyVersion } = await db.getLatestVersions(Environment.PRODUCTION); |
73 | 97 |
|
74 | | - expect(keyVersion).toBe(1); |
| 98 | + expect(keyVersion).toBe(1); |
75 | 99 |
|
76 | | - const asset = assets.filter((a) => a.name === 'aviaviavi'); |
77 | | - expect(asset).toHaveLength(1); |
78 | | - expect(asset[0]).toHaveProperty('version', 2); |
| 100 | + const asset = assets.filter((a) => a.name === 'aviaviavi'); |
| 101 | + expect(asset).toHaveLength(1); |
| 102 | + expect(asset[0]).toHaveProperty('version', 2); |
79 | 103 |
|
80 | | - const connection = connections.filter((c) => c.name === 'xd'); |
81 | | - expect(connection).toHaveLength(1); |
82 | | - expect(connection[0]).toHaveProperty('version', 2); |
| 104 | + const connection = connections.filter((c) => c.name === 'xd'); |
| 105 | + expect(connection).toHaveLength(1); |
| 106 | + expect(connection[0]).toHaveProperty('version', 2); |
| 107 | + }); |
83 | 108 | }); |
84 | | - }); |
85 | 109 |
|
86 | | - describe('#getBundleFromVersions', function () { |
87 | | - it('should fetch the bundle content based on the versions from the database', async function () { |
88 | | - const db = new BundleDatabase(dataSource); |
| 110 | + describe('#getBundleFromVersions', function () { |
| 111 | + it('should fetch the bundle content based on the versions from the database', async function () { |
| 112 | + const db = new BundleDatabase(dataSource); |
89 | 113 |
|
90 | | - const { assets } = await db.getBundleFromVersions({ |
91 | | - environment: Environment.PRODUCTION, |
92 | | - assets: [{ name: 'aviaviavi', version: 1 }], |
93 | | - connections: [], |
94 | | - keyVersion: 1, |
95 | | - }); |
| 114 | + const { assets } = await db.getBundleFromVersions({ |
| 115 | + environment: Environment.PRODUCTION, |
| 116 | + assets: [{ name: 'aviaviavi', version: 1 }], |
| 117 | + connections: [], |
| 118 | + keyVersion: 1, |
| 119 | + }); |
96 | 120 |
|
97 | | - expect(assets).toSatisfyAll<Asset>((a) => a.environment.includes(Environment.PRODUCTION)); |
98 | | - expect(assets[0]).toHaveProperty('version', 1); |
| 121 | + expect(assets).toSatisfyAll<Asset>((a) => a.environment.includes(Environment.PRODUCTION)); |
| 122 | + expect(assets[0]).toHaveProperty('version', 1); |
| 123 | + }); |
99 | 124 | }); |
100 | 125 | }); |
101 | 126 | }); |
0 commit comments