Skip to content

Commit

Permalink
Merge branch 'master' into feat/4175-idling-resource-refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gosha212 authored Dec 19, 2024
2 parents f511625 + f0f8995 commit 93c73c2
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 1,006 deletions.
Binary file removed artifacts__detoxtest_2024-10-15_10-04-31.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion detox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"caf": "^15.0.1",
"chalk": "^4.0.0",
"child-process-promise": "^2.2.0",
"detox-copilot": "^0.0.24",
"detox-copilot": "^0.0.27",
"execa": "^5.1.1",
"find-up": "^5.0.0",
"fs-extra": "^11.0.0",
Expand Down
17 changes: 11 additions & 6 deletions detox/src/DetoxWorker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const CAF = require('caf');
const copilot = require('detox-copilot').default;
const _ = require('lodash');

const Client = require('./client/Client');
Expand Down Expand Up @@ -62,7 +63,7 @@ class DetoxWorker {
/** @type {Detox.SystemFacade} */
this.system = null;
/** @type {Detox.DetoxCopilotFacade} */
this.copilot = null;
this.copilot = new DetoxCopilot();

this._deviceCookie = null;

Expand Down Expand Up @@ -126,8 +127,6 @@ class DetoxWorker {
runtimeDeviceFactory,
} = environmentFactory.createFactories(deviceConfig);

this.copilot = new DetoxCopilot();

const envValidator = envValidatorFactory.createValidator();
yield envValidator.validate();

Expand Down Expand Up @@ -226,9 +225,10 @@ class DetoxWorker {
yield this._artifactsManager.onRunDescribeStart(...args);
};

onTestStart = function* (_signal, testSummary) {
// Copilot is reset before each test to ensure a clean state
this.copilot.resetIfNeeded();
onTestStart = function* (_signal, testSummary){
if (copilot.isInitialized()) {
copilot.start();
}

this._validateTestSummary('beforeEach', testSummary);

Expand Down Expand Up @@ -257,6 +257,11 @@ class DetoxWorker {
pendingRequests: testSummary.timedOut,
testName: testSummary.fullName,
});

if (copilot.isInitialized()) {
// In case of failure, pass false to copilot, so temporary cache is not saved
copilot.end(testSummary.status === 'passed');
}
};

onRunDescribeFinish = function* (_signal, ...args) {
Expand Down
66 changes: 52 additions & 14 deletions detox/src/DetoxWorker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ const testSummaries = require('./artifacts/__mocks__/testSummaries.mock');
const configuration = require('./configuration');
const Deferred = require('./utils/Deferred');

jest.mock('./copilot/DetoxCopilot');
jest.mock('./utils/logger');
jest.mock('./client/Client');
jest.mock('./utils/AsyncEmitter');
jest.mock('./invoke');
jest.mock('./utils/wrapWithStackTraceCutter');
jest.mock('./environmentFactory');

let mockIsInitialized = false;
jest.mock('detox-copilot', () => ({
default: {
init: jest.fn(),
isInitialized: jest.fn(() => mockIsInitialized),
start: jest.fn(),
end: jest.fn(),
perform: jest.fn(),
},
}));

describe('DetoxWorker', () => {
const fakeCookie = {
chocolate: 'yum',
Expand Down Expand Up @@ -41,6 +51,7 @@ describe('DetoxWorker', () => {
let runtimeDevice;
let Detox;
let detox;
let copilot;

beforeEach(() => {
mockEnvironmentFactories();
Expand All @@ -56,6 +67,8 @@ describe('DetoxWorker', () => {
});

beforeEach(async () => {
mockIsInitialized = false;

detoxConfig = await configuration.composeDetoxConfig({
override: {
configurations: {
Expand Down Expand Up @@ -86,6 +99,8 @@ describe('DetoxWorker', () => {
[symbols.allocateDevice]: jest.fn().mockResolvedValue(fakeCookie),
[symbols.deallocateDevice]: jest.fn(),
};

copilot = require('detox-copilot').default;
});

describe('when DetoxWorker#init() is called', () => {
Expand Down Expand Up @@ -301,17 +316,14 @@ describe('DetoxWorker', () => {
await init();
});

it('should create a new DetoxCopilot instance', () => {
expect(DetoxCopilot).toHaveBeenCalledTimes(1);
});

it('should assign the DetoxCopilot instance to the copilot property', () => {
expect(detox.copilot).toBeDefined();
expect(detox.copilot).toBeInstanceOf(DetoxCopilot);
});

it('should not initialize the copilot', () => {
expect(detox.copilot.init).not.toHaveBeenCalled();
expect(copilot.init).not.toHaveBeenCalled();
});
});
});
Expand All @@ -334,24 +346,30 @@ describe('DetoxWorker', () => {
status: undefined,
})).rejects.toThrowError(/Invalid test summary status/);
});
});

describe('with a valid test summary', () => {
beforeEach(() => detox.onTestStart(testSummaries.running()));

it('should reset copilot if needed', async () => {
it('should notify artifacts manager about "testStart', () =>
expect(artifactsManager.onTestStart).toHaveBeenCalledWith(testSummaries.running()));

it('should not start copilot if copilot init was not called', async () => {
try {
await detox.onTestStart('Test');
} catch {}

expect(detox.copilot.resetIfNeeded).toHaveBeenCalled();
expect(copilot.start).not.toHaveBeenCalled();
});
});

describe('with a valid test summary', () => {
beforeEach(() => detox.onTestStart(testSummaries.running()));
it('should start copilot if copilot init was called', async () => {
mockIsInitialized = true;

it('should notify artifacts manager about "testStart', () =>
expect(artifactsManager.onTestStart).toHaveBeenCalledWith(testSummaries.running()));
try {
await detox.onTestStart('Test');
} catch {}

it('should reset copilot if needed', async () => {
expect(detox.copilot.resetIfNeeded).toHaveBeenCalled();
expect(copilot.start).toHaveBeenCalled();
});

it('should not relaunch app', async () => {
Expand Down Expand Up @@ -382,6 +400,10 @@ describe('DetoxWorker', () => {

it('should notify artifacts manager about "testDone"', () =>
expect(artifactsManager.onTestDone).toHaveBeenCalledWith(testSummaries.passed()));

it('should not end copilot if copilot init was not called', async () => {
expect(copilot.end).not.toHaveBeenCalled();
});
});

describe('with a failed test summary (due to failed asseration)', () => {
Expand All @@ -397,6 +419,22 @@ describe('DetoxWorker', () => {
it('should dump pending network requests', () =>
expect(client().dumpPendingRequests).toHaveBeenCalled());
});

it('should end copilot if copilot init was called', async () => {
mockIsInitialized = true;

await detox.onTestDone(testSummaries.passed());

expect(copilot.end).toHaveBeenCalledWith(true);
});

it('should end copilot without cache if copilot init was called', async () => {
mockIsInitialized = true;

await detox.onTestDone(testSummaries.failed());

expect(copilot.end).toHaveBeenCalledWith(false);
});
});

describe('when DetoxWorker#cleanup() is called', () => {
Expand Down
18 changes: 3 additions & 15 deletions detox/src/copilot/DetoxCopilot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,15 @@ const copilot = require('detox-copilot').default;

const detoxCopilotFrameworkDriver = require('./detoxCopilotFrameworkDriver');

/**
* @typedef {Object} Detox.DetoxCopilotFacade
*/
class DetoxCopilot {
constructor() {
this.isInitialized = false;
}

init(promptHandler) {
copilot.init({
frameworkDriver: detoxCopilotFrameworkDriver,
promptHandler: promptHandler
});

this.isInitialized = true;
}

resetIfNeeded() {
if (!this.isInitialized) {
// Copilot is not initialized, nothing to reset
return;
}

copilot.reset();
}

perform(...steps) {
Expand Down
23 changes: 4 additions & 19 deletions detox/src/copilot/DetoxCopilot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const detoxCopilotFrameworkDriver = require('./detoxCopilotFrameworkDriver');
jest.mock('detox-copilot', () => ({
default: {
init: jest.fn(),
reset: jest.fn(),
isInitialized: jest.fn().mockReturnValue(false),
start: jest.fn(),
end: jest.fn(),
perform: jest.fn(),
},
}));
Expand All @@ -23,30 +25,13 @@ describe('DetoxCopilot', () => {
});

describe('init', () => {
it('should initialize copilot with correct parameters', () => {
it.skip('should initialize copilot with correct parameters', () => {
detoxCopilot.init(mockPromptHandler);

expect(copilot.init).toHaveBeenCalledWith({
frameworkDriver: detoxCopilotFrameworkDriver,
promptHandler: mockPromptHandler,
});
expect(detoxCopilot.isInitialized).toBe(true);
});
});

describe('resetIfNeeded', () => {
it('should reset copilot if initialized', () => {
detoxCopilot.isInitialized = true;
detoxCopilot.resetIfNeeded();

expect(copilot.reset).toHaveBeenCalled();
});

it('should not reset copilot if not initialized', () => {
detoxCopilot.isInitialized = false;
detoxCopilot.resetIfNeeded();

expect(copilot.reset).not.toHaveBeenCalled();
});
});

Expand Down
Loading

0 comments on commit 93c73c2

Please sign in to comment.