import { pretty } from '../src/utils/helpers'; import { validateMessage, buildMessageForJS, buildMessage, validate, buildForJS, build, } from '../src/models/user'; const date = new Date(); const valids = [{ client: 'debug-client', source: 'flashman' as const, product: 'flashman', id: '123456789', name: 'abc', date, }, { client: 'debug-client', source: 'anlix' as const, product: 'flashsomething', id: '123456789', name: 'abc', date, }, { client: 'debug-client', source: 'control' as const, product: 'control', id: '123456789', name: 'abc', date, }]; const invalids = [{ v: { // missing 'client'. source: 'anlix' as const, product: 'flashlala', id: '123456789', name: 'abc', date, }, fs: 'all', }, { v: { client: 124253, // 'client' has wrong type. source: 'anlix' as const, product: 'flashlala', id: '123456789', name: 'abc', date, }, fs: 'all', }, { v: { client: null, // 'client' is null. source: 'anlix' as const, product: 'flashlala', id: '123456789', name: 'abc', date, }, fs: 'all', }, { v: { client: 'debug-client', id: '123456789', // missing 'product'. name: 'abc', date, }, fs: ['buildMessageForJS', 'buildForJS'], }, { v: { client: 'debug-client', id: '123456789', product: 2131231, // 'product' has wrong type. name: 'abc', date, }, fs: ['buildMessageForJS', 'buildForJS'], }, { v: { // missing 'source'. client: 'debug-client', id: '123456789', name: 'abc', date, }, fs: ['validateMessage', 'validate'], }, { v: { client: 'debug-client', source: 'lalala' as const, // unknown user database 'source'. id: '123456789', name: 'abc', date, }, fs: ['validateMessage', 'validate'], }, { v: { client: 'debug-client', source: 1232423, // wrong type for database 'source'. id: '123456789', name: 'abc', date, }, fs: ['validateMessage', 'validate'], }, { v: { client: 'debug-client', source: null, // database source field is null id: '123456789', name: 'abc', date, }, fs: ['validateMessage', 'validate'], }, { v: { // missing user 'id' field. client: 'debug-client', source: 'control' as const, product: 'control', name: 'abc', date, }, fs: 'all', }, { v: { client: 'debug-client', source: 'control' as const, product: 'control', id: null, // user 'id' field is null. name: 'abc', date, }, fs: 'all', }, { v: { client: 'debug-client', source: 'control' as const, product: 'control', id: 123234234, // wrong type for user 'id'. name: 'abc', date, }, fs: 'all', }, { v: { client: 'debug-client', source: 'control' as const, product: 'control', id: '123456789', // missing user 'name' field. date, }, fs: 'all', }, { v: { client: 'debug-client', source: 'control' as const, product: 'control', id: '123456789', name: null, // field 'name' is null. date, }, fs: 'all', }, { v: { client: 'debug-client', source: 'control' as const, product: 'control', id: '123456789', name: true, // wrong type for 'name'. date, }, fs: 'all', }, { v: { client: 'debug-client', source: 'control' as const, product: 'control', id: '123456789', name: 'abc', // missing 'date'. }, fs: ['validate'], }, { v: { client: 'debug-client', source: 'control' as const, product: 'control', id: '123456789', name: 'abc', date: new Date(0), // insane value for 'date'. }, fs: ['validate'], }, { v: { client: 'debug-client', source: 'control' as const, product: 'control', id: '123456789', name: 'abc', date: new Date(0).toISOString(), // wrong type for 'date'. }, fs: ['validate'], }]; let success = true; // testing valid Users. for (let i = 0; i < valids.length; i++) { let u = valids[i]; const functions = []; let err1 = validateMessage({ _id: {client: u.client, source: u.source, id: u.id}, name: u.name, }); if (err1) functions.push('validateMessage()'); let [new2, err2] = buildMessageForJS(u.client, u.product, u.id, u.name); if (err2 && !new2) functions.push('buildMessageForJS()'); let [new3, err3] = buildMessage(u.client, u.product, u.id, u.name); if (err3 && !new3) functions.push('buildMessage()'); if (functions.length > 0) { success = false; console.log( `Valid UserMessage ${i+1}, out of ${valids.length}, was not validated `+ `for functions [${functions.join(', ')}]:`, pretty(u, true), err1 || err2 || err3, ); } } // testing invalid Users. for (let i = 0; i < invalids.length; i++) { let invalid = invalids[i] let u = invalid.v; let fs = invalid.fs; const functions = []; if (fs === 'all' || fs.includes('validateMessage')) { let err = validateMessage({ _id: {client: u.client, source: u.source, id: u.id}, name: u.name, }); if (!err) functions.push('validateMessage()'); } if (fs === 'all' || fs.includes('validate')) { let err = validate({ _id: {client: u.client, source: u.source, id: u.id}, name: u.name, date: u.date, }); if (!err) functions.push('validate()'); } if (fs === 'all' || fs.includes('buildMessageForJS')) { let [newU, err] = buildMessageForJS(u.client, u.product, u.id, u.name); if (!err && newU) functions.push('buildMessageForJS()'); } if (fs === 'all' || fs.includes('buildForJS')) { let [newU, err] = buildForJS(u.client, u.product, u.id, u.name, u.date); if (!err && newU) functions.push('buildForJS()'); } // We can't check against the model 'user.build' function because the typescript transpiler // will complain against forced type errors and this test will not even run. if (functions.length > 0) { success = false; console.log( `Invalid UserMessage ${i+1}, out of ${invalids.length}, was not invalidated `+ `for functions [${functions.join(', ')}]:`, pretty(u, true), ); } } export default success;