const chai = require('chai'); const chaiHttp = require('chai-http'); const express = require('express'); import { expect } from 'chai'; import { SetupApp } from '../../../app'; import { ReferenceDatabase } from '../../reference_model/DBReferenceModel'; import { randomDBAction, RandomDBActionDistribution, DBActionType, applyReferenceDBAction, applyRealDBAction, DBAction } from '../../reference_model/randomGen'; import * as helpers from '../helpers'; import seedrandom from 'seedrandom'; import { AlbumWithRefsWithId, Artist, ArtistWithRefsWithId, TagWithRefsWithId, TrackWithRefs, TrackWithRefsWithId } from '../../../../client/src/api/api'; import sampleDB from '../sampleDB'; let stringify = require('json-stringify-deterministic'); let _ = require('lodash'); let tmp = require('tmp'); let fs = require('fs'); async function init() { chai.use(chaiHttp); const app = express(); const knex = await helpers.initTestDB(); SetupApp(app, knex, ''); var agent = chai.request.agent(app); return agent; } // Alters a response from a real or mock DB so that they can be deep-compared // and only non-trivial differences trigger an error. function normalizeResponse(response: any) { let r: any = _.cloneDeep(response); if (r && 'id' in r) { r.id = ''; } return r; } // Alters a database export / reference database model so that it can be compared // to another so that only non-trivial differences trigger an error. function normalizeDB(oldDb: ReferenceDatabase) { let db: ReferenceDatabase = _.cloneDeep(oldDb); // Apply a deterministic sorting. // TODO: sorting by name is not deterministic. for (const userId in db) { db[userId].tracks.sort((a: any, b: any) => a.name.localeCompare(b.name)) db[userId].albums.sort((a: any, b: any) => a.name.localeCompare(b.name)) db[userId].artists.sort((a: any, b: any) => a.name.localeCompare(b.name)) db[userId].tags.sort((a: any, b: any) => a.name.localeCompare(b.name)) } // Re-map IDs. interface IDMap { map: Map, highestId: number, }; let trackMap: IDMap = { map: new Map(), highestId: 0 }; let albumMap: IDMap = { map: new Map(), highestId: 0 }; let artistMap: IDMap = { map: new Map(), highestId: 0 }; let tagMap: IDMap = { map: new Map(), highestId: 0 }; let remapId = (id: number, map: IDMap) => { if (map.map.has(id)) { return map.map.get(id) as number; } let newId: number = map.highestId + 1; map.map.set(id, newId); map.highestId = newId; return newId; } for (const userId in db) { // First remap the IDs only, ignoring references db[userId].tracks.forEach((x: TrackWithRefsWithId) => { console.log("X:", x); x.id = remapId(x.id, trackMap); }); db[userId].albums.forEach((x: AlbumWithRefsWithId) => { x.id = remapId(x.id, albumMap); }) db[userId].artists.forEach((x: ArtistWithRefsWithId) => { x.id = remapId(x.id, artistMap); }) db[userId].tags.forEach((x: TagWithRefsWithId) => { x.id = remapId(x.id, tagMap); }) } for (const userId in db) { // Now remap the references. db[userId].tracks.forEach((x: TrackWithRefsWithId) => { x.tagIds = x.tagIds.map((id: number) => remapId(id, tagMap)); x.artistIds = x.artistIds.map((id: number) => remapId(id, artistMap)); x.albumId = x.albumId ? remapId(x.albumId, albumMap) : null; }); db[userId].albums.forEach((x: AlbumWithRefsWithId) => { x.tagIds = x.tagIds.map((id: number) => remapId(id, tagMap)); x.artistIds = x.artistIds.map((id: number) => remapId(id, artistMap)); x.trackIds = x.trackIds.map((id: number) => remapId(id, trackMap)); }); db[userId].artists.forEach((x: ArtistWithRefsWithId) => { x.tagIds = x.tagIds.map((id: number) => remapId(id, tagMap)); x.albumIds = x.albumIds.map((id: number) => remapId(id, albumMap)); x.trackIds = x.trackIds.map((id: number) => remapId(id, trackMap)); }); db[userId].tags.forEach((x: TagWithRefsWithId) => { x.parentId = x.parentId ? remapId(x.parentId, tagMap) : null; }); } return db; } describe('Randomized model-based DB back-end tests', () => { it('all succeed', async done => { let req = await init(); let actionTrace: DBAction[] = []; let seed: string = process.env.TEST_RANDOM_SEED || Math.random().toFixed(5).toString(); console.log(`Test random seed: '${seed}'`) try { // Create a reference DB let refDB: ReferenceDatabase = _.cloneDeep(sampleDB); // Prime the real DB // First, create a user and log in. await helpers.createUser(req, "someone@email.com", "password1A!", 200); await helpers.login(req, "someone@email.com", "password1A!", 200); // Import the starting DB. await helpers.importDB(req, refDB[1]); // Check that we are starting from an equal situation let refState = normalizeDB(refDB); let realState = normalizeDB({ [1]: (await helpers.getExport(req)).body, }); expect(realState).to.deep.equal(refState); // Start doing some random changes, checking the state after each step. let rng = seedrandom(seed); let dist: RandomDBActionDistribution = { type: new Map([ [DBActionType.CreateTrack, 0.7], [DBActionType.DeleteTrack, 0.3] ]), userId: new Map([[1, 1.0]]), createTrackParams: { linkAlbum: new Map([[false, 0.45], [true, 0.45], ['nonexistent', 0.1]]), linkTags: { numValid: new Map([[0, 1.0]]), numInvalid: new Map([[0, 0.9], [1, 0.05], [2, 0.05]]), }, linkArtists: { numValid: new Map([[0, 1.0]]), numInvalid: new Map([[0, 0.9], [1, 0.05], [2, 0.05]]), }, }, deleteTrackParams: { validTrack: new Map([[false, 0.2], [true, 0.8]]) } } for (let i = 0; i < 30; i++) { let action = randomDBAction( refDB, rng, dist ); actionTrace.push(action); console.log("Testing action: ", action); let { response: refResponse, status: refStatus } = applyReferenceDBAction(action, refDB); let { response: realResponse, status: realStatus } = await applyRealDBAction(action, req); // Compare the response and status. expect(normalizeResponse(realResponse)).to.deep.equal(normalizeResponse(refResponse)); expect(realStatus).to.equal(refStatus); // Compare the database state after the action. let refState = normalizeDB(refDB); let realState = normalizeDB({ [1]: (await helpers.getExport(req)).body, }); expect(realState).to.deep.equal(refState); } } catch (e) { // When catching a comparison error, add and dump various states to files for debugging. e.actionTrace = actionTrace; e.startingDB = normalizeDB(sampleDB); e.testSeed = seed; if (e.actual && e.expected) { e.actualDump = tmp.tmpNameSync(); e.expectedDump = tmp.tmpNameSync(); e.actionTraceDump = tmp.tmpNameSync(); e.startingDBDump = tmp.tmpNameSync(); fs.writeFileSync(e.actualDump, stringify(e.actual, { space: ' ' })); fs.writeFileSync(e.expectedDump, stringify(e.expected, { space: ' ' })); fs.writeFileSync(e.actionTraceDump, stringify(e.actionTrace, { space: ' ' })); fs.writeFileSync(e.startingDBDump, stringify(e.startingDB, { space: ' ' })); console.log( "A comparison error occurred. Wrote compared values to temporary files for debugging:\n" + ` actual: ${e.actualDump}\n` + ` expected: ${e.expectedDump}\n` + ` DB action trace: ${e.actionTraceDump}\n` + ` Starting DB: ${e.startingDBDump}` ); } throw e; } finally { req.close(); done(); } }); });