blob: a0a68b60da076df1ff99e06df3aab171fc9d4a14 [file]
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {testResolver} from '../../test/common-test-setup';
import {
FlowsModel,
getChangePrefix,
getSubmitCondition,
SUBMIT_ACTION_NAME,
} from './flows-model';
import {ChangeModel, changeModelToken} from '../change/change-model';
import {PluginsModel} from '../plugins/plugins-model';
import {stubRestApi, waitUntil} from '../../test/test-utils';
import {NumericChangeId} from '../../types/common';
import {assert} from '@open-wc/testing';
import {FlowsAutosubmitProvider} from '../../api/flows';
import {createFlow, createParsedChange} from '../../test/test-data-generators';
import {FlowInfo, FlowStageState} from '../../api/rest-api';
suite('flows-model tests', () => {
let flowsModel: FlowsModel;
let changeModel: ChangeModel;
let pluginsModel: PluginsModel;
setup(() => {
changeModel = testResolver(changeModelToken);
pluginsModel = new PluginsModel();
flowsModel = new FlowsModel(changeModel, pluginsModel);
});
test('flows$ loads flows when enabled', async () => {
stubRestApi('getIfFlowsIsEnabled').resolves({enabled: true});
const listFlowsStub = stubRestApi('listFlows').resolves([
createFlow({uuid: 'flow1', stages: []}),
]);
let flows: FlowInfo[] = [];
flowsModel.flows$.subscribe(f => (flows = f));
changeModel.updateStateChange({
...createParsedChange(),
_number: 123 as NumericChangeId,
});
await waitUntil(() => flows.length > 0);
assert.equal(flows.length, 1);
assert.equal(flows[0].uuid, 'flow1');
assert.isTrue(listFlowsStub.calledWith(123));
});
test('deleteFlow calls API and reloads', async () => {
stubRestApi('getIfFlowsIsEnabled').resolves({enabled: true});
stubRestApi('listFlows').resolves([]);
const deleteFlowStub = stubRestApi('deleteFlow').resolves(new Response());
const reloadSpy = sinon.spy(flowsModel, 'reload');
changeModel.updateStateChange({
...createParsedChange(),
_number: 123 as NumericChangeId,
});
await waitUntil(() => flowsModel.getState().isEnabled);
await flowsModel.deleteFlow('flow1');
assert.isTrue(deleteFlowStub.calledWith(123, 'flow1'));
assert.isTrue(reloadSpy.called);
});
test('createFlow calls API and reloads', async () => {
stubRestApi('getIfFlowsIsEnabled').resolves({enabled: true});
stubRestApi('listFlows').resolves([]);
const createFlowStub = stubRestApi('createFlow').resolves(
createFlow({uuid: 'new-flow', stages: []})
);
const reloadSpy = sinon.spy(flowsModel, 'reload');
changeModel.updateStateChange({
...createParsedChange(),
_number: 123 as NumericChangeId,
});
await waitUntil(() => flowsModel.getState().isEnabled);
const flowInput = {stage_expressions: []};
const created = await flowsModel.createFlow(flowInput);
assert.isTrue(createFlowStub.calledWith(123, flowInput));
assert.equal(created?.uuid, 'new-flow');
assert.isTrue(reloadSpy.called);
});
test('createFlow awaits enabled$ resolution and respects reload=false', async () => {
stubRestApi('getIfFlowsIsEnabled').resolves({enabled: true});
stubRestApi('listFlows').resolves([]);
const createFlowStub = stubRestApi('createFlow').resolves(
createFlow({uuid: 'new-flow', stages: []})
);
const reloadSpy = sinon.spy(flowsModel, 'reload');
changeModel.updateStateChange({
...createParsedChange(),
_number: 123 as NumericChangeId,
});
const flowInput = {
stage_expressions: [
{
condition: 'wait 1d',
action: {name: 'add-reviewer', parameters: ['human@example.com']},
},
],
};
const created = await flowsModel.createFlow(flowInput, undefined, false);
assert.equal(created?.uuid, 'new-flow');
assert.isFalse(reloadSpy.called);
assert.deepEqual(createFlowStub.firstCall.args[1], flowInput);
});
test('deleteFlow invokes errFn and skips reload on non-2xx response', async () => {
stubRestApi('getIfFlowsIsEnabled').resolves({enabled: true});
stubRestApi('listFlows').resolves([]);
stubRestApi('deleteFlow').resolves(
new Response('Not found', {status: 404})
);
const reloadSpy = sinon.spy(flowsModel, 'reload');
const errSpy = sinon.spy();
changeModel.updateStateChange({
...createParsedChange(),
_number: 123 as NumericChangeId,
});
await flowsModel.deleteFlow('flow1', errSpy);
assert.isTrue(errSpy.calledOnce);
assert.isFalse(reloadSpy.called);
});
test('deleteFlow fires server-error and skips reload on non-2xx response when errFn is omitted', async () => {
stubRestApi('getIfFlowsIsEnabled').resolves({enabled: true});
stubRestApi('listFlows').resolves([]);
stubRestApi('deleteFlow').resolves(
new Response('Server error', {status: 500})
);
const reloadSpy = sinon.spy(flowsModel, 'reload');
const serverErrorStub = sinon.stub();
document.addEventListener('server-error', serverErrorStub);
try {
changeModel.updateStateChange({
...createParsedChange(),
_number: 123 as NumericChangeId,
});
await flowsModel.deleteFlow('flow1');
assert.isTrue(serverErrorStub.calledOnce);
assert.isFalse(reloadSpy.called);
} finally {
document.removeEventListener('server-error', serverErrorStub);
}
});
test('createFlow resolves undefined if changeNum is cleared while enabled$ is pending', async () => {
stubRestApi('getIfFlowsIsEnabled').returns(new Promise(() => {}));
const createFlowStub = stubRestApi('createFlow');
const errSpy = sinon.spy();
changeModel.updateStateChange({
...createParsedChange(),
_number: 123 as NumericChangeId,
});
const createPromise = flowsModel.createFlow(
{stage_expressions: []},
errSpy
);
changeModel.updateState({change: undefined});
const created = await createPromise;
assert.isUndefined(created);
assert.isTrue(errSpy.calledOnce);
assert.isFalse(createFlowStub.called);
});
test('createFlow does not reuse stale isEnabled from previous change', async () => {
stubRestApi('getIfFlowsIsEnabled').callsFake(changeNum =>
Promise.resolve({enabled: changeNum === (123 as NumericChangeId)})
);
stubRestApi('listFlows').resolves([]);
const createFlowStub = stubRestApi('createFlow');
const errSpy = sinon.spy();
changeModel.updateStateChange({
...createParsedChange(),
_number: 123 as NumericChangeId,
});
await waitUntil(() => flowsModel.getState().isEnabled);
changeModel.updateState({
change: {
...createParsedChange(),
_number: 456 as NumericChangeId,
},
});
const created = await flowsModel.createFlow(
{stage_expressions: []},
errSpy
);
assert.isUndefined(created);
assert.isTrue(errSpy.calledOnce);
assert.isFalse(createFlowStub.called);
});
test('createAutosubmitFlow uses default values', async () => {
stubRestApi('getIfFlowsIsEnabled').resolves({enabled: true});
stubRestApi('listFlows').resolves([]);
const createFlowStub = stubRestApi('createFlow').resolves(
createFlow({uuid: 'auto-flow', stages: []})
);
changeModel.updateStateChange({
...createParsedChange(),
_number: 123 as NumericChangeId,
});
await waitUntil(() => flowsModel.getState().isEnabled);
await flowsModel.createAutosubmitFlow();
assert.isTrue(createFlowStub.called);
const args = createFlowStub.firstCall.args;
assert.equal(args[0], 123 as NumericChangeId);
assert.deepEqual(args[1], {
stage_expressions: [
{
condition: getSubmitCondition(),
action: {name: SUBMIT_ACTION_NAME},
},
],
});
});
test('createAutosubmitFlow uses provider values', async () => {
stubRestApi('getIfFlowsIsEnabled').resolves({enabled: true});
stubRestApi('listFlows').resolves([]);
const createFlowStub = stubRestApi('createFlow').resolves(
createFlow({uuid: 'auto-flow', stages: []})
);
const provider: FlowsAutosubmitProvider = {
isAutosubmitEnabled: () => true,
getSubmitCondition: () => 'custom condition',
getSubmitAction: () => {
return {name: 'custom action'};
},
};
pluginsModel.registerFlowsAutosubmitProvider({
pluginName: 'test',
provider,
});
changeModel.updateStateChange({
...createParsedChange(),
_number: 123 as NumericChangeId,
});
await waitUntil(() => flowsModel.getState().isEnabled);
await flowsModel.createAutosubmitFlow();
assert.isTrue(createFlowStub.called);
const args = createFlowStub.firstCall.args;
assert.deepEqual(args[1], {
stage_expressions: [
{
condition: getChangePrefix() + ' is custom condition',
action: {name: 'custom action'},
},
],
});
});
test('hasAutosubmitFlowAlready checks flows', async () => {
stubRestApi('getIfFlowsIsEnabled').resolves({enabled: true});
const listFlowsStub = stubRestApi('listFlows').resolves([
createFlow({
uuid: 'flow1',
stages: [
{
expression: {
condition: getSubmitCondition(),
action: {name: SUBMIT_ACTION_NAME},
},
state: FlowStageState.DONE,
},
],
}),
]);
changeModel.updateStateChange({
...createParsedChange(),
_number: 123 as NumericChangeId,
});
await waitUntil(() => flowsModel.getState().flows.length > 0);
assert.isTrue(flowsModel.hasAutosubmitFlowAlready());
listFlowsStub.resolves([
createFlow({
uuid: 'flow2',
stages: [
{
expression: {
condition: 'other condition',
action: {name: 'other action'},
},
state: FlowStageState.DONE,
},
],
}),
]);
flowsModel.reload();
await waitUntil(() => !flowsModel.hasAutosubmitFlowAlready());
assert.isFalse(flowsModel.hasAutosubmitFlowAlready());
});
suite('getChangePrefix', () => {
let originalPath: string;
setup(() => {
originalPath = window.location.pathname;
});
teardown(() => {
window.history.replaceState(null, '', originalPath);
});
test('strips patchset, diff range, and file path subpaths', () => {
const origin = window.location.origin;
window.history.replaceState(null, '', '/c/my-repo/+/123');
assert.equal(getChangePrefix(), `${origin}/c/my-repo/+/123`);
window.history.replaceState(null, '', '/c/my-repo/+/123/4');
assert.equal(getChangePrefix(), `${origin}/c/my-repo/+/123`);
window.history.replaceState(null, '', '/c/my-repo/+/123/1..4');
assert.equal(getChangePrefix(), `${origin}/c/my-repo/+/123`);
window.history.replaceState(
null,
'',
'/c/my-repo/+/123/1..4/src/file.ts'
);
assert.equal(getChangePrefix(), `${origin}/c/my-repo/+/123`);
});
});
test('hasAutosubmitFlowAlready detects flows when navigating between patchsets/diff ranges', async () => {
const originalPath = window.location.pathname;
try {
window.history.replaceState(null, '', '/c/my-repo/+/123/4..5/src/foo.ts');
stubRestApi('getIfFlowsIsEnabled').resolves({enabled: true});
const expectedPrefix = `${window.location.origin}/c/my-repo/+/123`;
stubRestApi('listFlows').resolves([
createFlow({
uuid: 'flow1',
stages: [
{
expression: {
condition: `${expectedPrefix} is is:submittable`,
action: {name: SUBMIT_ACTION_NAME},
},
state: FlowStageState.DONE,
},
],
}),
]);
changeModel.updateStateChange({
...createParsedChange(),
_number: 123 as NumericChangeId,
});
await waitUntil(() => flowsModel.getState().flows.length > 0);
assert.isTrue(flowsModel.hasAutosubmitFlowAlready());
} finally {
window.history.replaceState(null, '', originalPath);
}
});
});