|
| 1 | +'use strict'; |
| 2 | +const chai = require('chai'); |
| 3 | +const expect = chai.expect; |
| 4 | +const sinon = require('sinon'); |
| 5 | +const fs = require('fs'); |
| 6 | +const os = require('os'); |
| 7 | +const path = require('path'); |
| 8 | + |
| 9 | +const o11yHelper = require('../../../../bin/testObservability/helper/helper'); |
| 10 | +const a11yHelper = require('../../../../bin/accessibility-automation/helper'); |
| 11 | +const baseHelper = require('../../../../bin/helpers/helper'); |
| 12 | + |
| 13 | +// Regression guard for SDK-7121: the support-file instrumentation MUST land |
| 14 | +// synchronously. runs.js calls setEventListeners(bsConfig) and then proceeds |
| 15 | +// immediately to md5 hashing + zip archiving. When the injection was deferred to |
| 16 | +// an async glob callback, it raced the archive — a lost race shipped an |
| 17 | +// un-instrumented suite, and md5 caching made it sticky, so the new Automate |
| 18 | +// dashboard (TRA) received zero test events. |
| 19 | +describe('SDK-7121 synchronous support-file instrumentation', () => { |
| 20 | + let tmpDir, supportPath, cwdStub, getSupportFilesStub; |
| 21 | + |
| 22 | + const setupTmpProject = () => { |
| 23 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sdk7121-')); |
| 24 | + fs.mkdirSync(path.join(tmpDir, 'cypress', 'support'), { recursive: true }); |
| 25 | + supportPath = path.join(tmpDir, 'cypress', 'support', 'e2e.js'); |
| 26 | + fs.writeFileSync(supportPath, '// user original support file\n'); |
| 27 | + cwdStub = sinon.stub(process, 'cwd').returns(tmpDir); |
| 28 | + }; |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + if (cwdStub) cwdStub.restore(); |
| 32 | + if (getSupportFilesStub) getSupportFilesStub.restore(); |
| 33 | + if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 34 | + cwdStub = getSupportFilesStub = tmpDir = undefined; |
| 35 | + }); |
| 36 | + |
| 37 | + describe('testObservability setEventListeners', () => { |
| 38 | + beforeEach(() => { |
| 39 | + setupTmpProject(); |
| 40 | + process.env.BS_TESTOPS_BUILD_COMPLETED = 'true'; |
| 41 | + // non-magic path -> glob.sync resolves the exact file |
| 42 | + getSupportFilesStub = sinon.stub(baseHelper, 'getSupportFiles').returns({ |
| 43 | + supportFile: '/cypress/support/e2e.js', |
| 44 | + cleanupParams: {} |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('injects the observability require synchronously before returning', () => { |
| 49 | + o11yHelper.setEventListeners({ run_settings: {} }); |
| 50 | + // Read exactly as md5/archive would — synchronously, right after the call. |
| 51 | + const content = fs.readFileSync(supportPath, 'utf-8'); |
| 52 | + expect(content).to.include('browserstack-cypress-cli/bin/testObservability/cypress'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('does not double-inject when called twice (idempotent)', () => { |
| 56 | + o11yHelper.setEventListeners({ run_settings: {} }); |
| 57 | + o11yHelper.setEventListeners({ run_settings: {} }); |
| 58 | + const content = fs.readFileSync(supportPath, 'utf-8'); |
| 59 | + const occurrences = content.split('browserstack-cypress-cli/bin/testObservability/cypress').length - 1; |
| 60 | + expect(occurrences).to.equal(1); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('accessibility setAccessibilityEventListeners (glob-pattern branch)', () => { |
| 65 | + beforeEach(() => { |
| 66 | + setupTmpProject(); |
| 67 | + // magic pattern -> exercises the glob.sync branch fixed for SDK-7121 |
| 68 | + getSupportFilesStub = sinon.stub(baseHelper, 'getSupportFiles').returns({ |
| 69 | + supportFile: '/cypress/support/**/*.js', |
| 70 | + cleanupParams: {} |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('injects the accessibility require synchronously before returning', () => { |
| 75 | + a11yHelper.setAccessibilityEventListeners({ run_settings: {} }); |
| 76 | + const content = fs.readFileSync(supportPath, 'utf-8'); |
| 77 | + expect(content).to.include('browserstack-cypress-cli/bin/accessibility-automation/cypress'); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments