|
| 1 | +import { |
| 2 | + skipIfApiStrict, |
| 3 | + startSharedTestServer, |
| 4 | +} from '../../../testing/integration-testing-hooks'; |
| 5 | +import { TestShell } from './test-shell'; |
| 6 | +import { expect } from 'chai'; |
| 7 | + |
| 8 | +const setDifference = <T>(a: T[], b: T[]) => a.filter((e) => !b.includes(e)); |
| 9 | +const expectIsSubset = <T>(a: T[], b: T[]) => |
| 10 | + expect(setDifference(a, b)).to.have.lengthOf(0); |
| 11 | +const commonPrefix = (a: string, b: string): string => |
| 12 | + a.startsWith(b) |
| 13 | + ? b |
| 14 | + : b.startsWith(a) |
| 15 | + ? a |
| 16 | + : b && commonPrefix(a, b.slice(0, -1)); |
| 17 | + |
| 18 | +describe('e2e startup banners', function () { |
| 19 | + skipIfApiStrict(); |
| 20 | + afterEach(TestShell.cleanup); |
| 21 | + |
| 22 | + const testServer = startSharedTestServer(); |
| 23 | + |
| 24 | + context('modules included in snapshots', function () { |
| 25 | + it('includes the right modules at the right point in time', async function () { |
| 26 | + if (!process.env.MONGOSH_TEST_EXECUTABLE_PATH) return this.skip(); |
| 27 | + |
| 28 | + const connectionString = await testServer.connectionString(); |
| 29 | + const helperScript = ` |
| 30 | + const S = process.__mongosh_webpack_stats; |
| 31 | + const L = (list) => list.map(S.lookupNaturalModuleName).filter(name => name && !name.endsWith('.json')); |
| 32 | + `; |
| 33 | + const commonArgs = ['--quiet', '--json=relaxed', '--eval', helperScript]; |
| 34 | + const argLists = [ |
| 35 | + [...commonArgs, '--nodb', '--eval', 'L(S.enumerateAllModules())'], |
| 36 | + [...commonArgs, '--nodb', '--eval', 'L(S.enumerateSnapshotModules())'], |
| 37 | + [...commonArgs, '--nodb', '--eval', 'L(S.enumerateLoadedModules())'], |
| 38 | + [ |
| 39 | + ...commonArgs, |
| 40 | + connectionString, |
| 41 | + '--eval', |
| 42 | + 'L(S.enumerateLoadedModules())', |
| 43 | + ], |
| 44 | + [ |
| 45 | + ...commonArgs, |
| 46 | + connectionString, |
| 47 | + '--jsContext=repl', |
| 48 | + '--eval', |
| 49 | + 'L(S.enumerateLoadedModules())', |
| 50 | + ], |
| 51 | + ]; |
| 52 | + const [ |
| 53 | + all, |
| 54 | + atSnapshotTime, |
| 55 | + atNodbEvalTime, |
| 56 | + atDbEvalTime, |
| 57 | + atReplEvalTime, |
| 58 | + ] = ( |
| 59 | + await Promise.all( |
| 60 | + argLists.map((args) => |
| 61 | + TestShell.runAndGetOutputWithoutErrors({ args }) |
| 62 | + ) |
| 63 | + ) |
| 64 | + ).map((output) => |
| 65 | + (JSON.parse(output) as string[]) |
| 66 | + .sort() |
| 67 | + .map((pkg) => pkg.replace(/\\/g, '/')) |
| 68 | + ); |
| 69 | + |
| 70 | + // Ensure that: atSnapshotTime ⊆ atNodbEvalTime ⊆ atDbEvalTime ⊆ atReplEvalTime ⊆ all |
| 71 | + expectIsSubset(atSnapshotTime, atNodbEvalTime); |
| 72 | + expectIsSubset(atNodbEvalTime, atDbEvalTime); |
| 73 | + expectIsSubset(atDbEvalTime, atReplEvalTime); |
| 74 | + expectIsSubset(atReplEvalTime, all); |
| 75 | + |
| 76 | + const prefix = all.reduce(commonPrefix); |
| 77 | + const stripPrefix = (s: string) => |
| 78 | + s.startsWith(prefix) ? s.replace(prefix, '') : s; |
| 79 | + |
| 80 | + const categorized = [ |
| 81 | + ...atSnapshotTime.map(stripPrefix).map((m) => [m, 'snapshot'] as const), |
| 82 | + ...setDifference(atNodbEvalTime, atSnapshotTime) |
| 83 | + .map(stripPrefix) |
| 84 | + .map((m) => [m, 'nodb-eval'] as const), |
| 85 | + ...setDifference(atDbEvalTime, atNodbEvalTime) |
| 86 | + .map(stripPrefix) |
| 87 | + .map((m) => [m, 'db-eval'] as const), |
| 88 | + ...setDifference(atReplEvalTime, atDbEvalTime) |
| 89 | + .map(stripPrefix) |
| 90 | + .map((m) => [m, 'repl-eval'] as const), |
| 91 | + ...setDifference(all, atReplEvalTime) |
| 92 | + .map(stripPrefix) |
| 93 | + .map((m) => [m, 'not-loaded'] as const), |
| 94 | + ]; |
| 95 | + |
| 96 | + // This is very helpful for inspecting snapshotted contents manually: |
| 97 | + // console.table(categorized.map(([m, c]) => [m.replace(prefix, ''), c])); |
| 98 | + const verifyAllInCategoryMatch = ( |
| 99 | + category: (typeof categorized)[number][1], |
| 100 | + re: RegExp |
| 101 | + ) => { |
| 102 | + for (const [module, cat] of categorized) { |
| 103 | + if (cat === category) { |
| 104 | + expect(module).to.match( |
| 105 | + re, |
| 106 | + `Found unexpected '${module}' in category '${cat}'` |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | + }; |
| 111 | + const verifyAllThatMatchAreInCategory = ( |
| 112 | + category: (typeof categorized)[number][1], |
| 113 | + re: RegExp |
| 114 | + ) => { |
| 115 | + for (const [module, cat] of categorized) { |
| 116 | + if (re.test(module)) { |
| 117 | + expect(cat).to.equal( |
| 118 | + category, |
| 119 | + `Expected '${module}' to be in category '${category}', actual category is '${cat}'` |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + // The core test: Verify that in the categories beyond 'not loaded at all' |
| 126 | + // and 'part of the snapshot', only a very specific set of modules is present, |
| 127 | + // and that some modules are only in specific categories. |
| 128 | + verifyAllInCategoryMatch('repl-eval', /^node_modules\/pretty-repl\//); |
| 129 | + verifyAllInCategoryMatch( |
| 130 | + 'db-eval', |
| 131 | + /^node_modules\/(kerberos|os-dns-native|resolve-mongodb-srv)\// |
| 132 | + ); |
| 133 | + verifyAllInCategoryMatch( |
| 134 | + 'nodb-eval', |
| 135 | + /^node_modules\/(kerberos|mongodb-client-encryption)\// |
| 136 | + ); |
| 137 | + verifyAllThatMatchAreInCategory( |
| 138 | + 'not-loaded', |
| 139 | + /^node_modules\/(express|openid-client|qs|send|jose|execa|body-parser|@babel\/highlight|@babel\/code-frame)\// |
| 140 | + ); |
| 141 | + verifyAllThatMatchAreInCategory( |
| 142 | + 'snapshot', |
| 143 | + /^node_modules\/(@babel\/types|@babel\/traverse|@mongodb-js\/devtools-connect|mongodb)\/|^packages\// |
| 144 | + ); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments