Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/desktop/src/main/__tests__/goal-arm-outcome.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import type { GoalArmOutcome } from '../../shared/goal-arm.js';
import { interpretGoalArmOutcome } from '../../renderer/goal-arm-outcome.js';
import { interpretGoalArmOutcome } from '../../renderer/features/goals/testing.js';
import { getShellCopy } from '../../renderer/locales/shell-copy.js';

test('successful Goal arming closes while every reconciliation result locks the form', () => {
Expand Down
277 changes: 277 additions & 0 deletions apps/desktop/src/main/__tests__/goal-controller.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { strict as assert } from 'node:assert';
import { afterEach, describe, it } from 'node:test';
import { act, createElement } from 'react';
import type { GoalState, GoalStatus } from '@maka/core/goal';
import { LocaleProvider } from '@maka/ui';
import { cleanupFakeDom, installReactRenderer } from './fake-dom.js';
import {
createFakeGoalServices,
GoalServicesProvider,
useGoalController,
type GoalController,
type GoalServices,
type UseGoalControllerInput,
} from '../../renderer/features/goals/testing.js';

function goal(
sessionId: string,
status: GoalStatus = 'active',
): GoalState {
return {
id: `goal-${sessionId}`,
revision: 1,
sessionId,
condition: `Finish ${sessionId}`,
status,
setAt: 100,
iterations: 2,
maxIterations: 9,
consecutiveNoProgress: 0,
blockCap: 3,
tokenBudget: 500,
tokensAtStart: 10,
tokensNow: 60,
tokensBaselinePending: false,
...(status === 'paused' ? { pausedAt: 200 } : {}),
};
}

function deferred<T>() {
let resolve!: (value: T) => void;
let reject!: (error: unknown) => void;
const promise = new Promise<T>((next, fail) => {
resolve = next;
reject = fail;
});
return { promise, resolve, reject };
}

let latestController: GoalController | undefined;

function ControllerProbe(props: UseGoalControllerInput) {
latestController = useGoalController(props);
return null;
}

function renderController(
root: ReturnType<typeof installReactRenderer>['root'],
services: GoalServices,
input: UseGoalControllerInput,
) {
root.render(
createElement(LocaleProvider, {
locale: 'en',
children: createElement(
GoalServicesProvider,
{ services },
createElement(ControllerProbe, input),
),
}),
);
}

function controller(): GoalController {
assert.ok(latestController);
return latestController;
}

function input(
activeSessionId: string | undefined,
errors: string[] = [],
): UseGoalControllerInput {
return {
activeSessionId,
reportError: (sessionId, title, description) => {
errors.push(`${sessionId}: ${title}: ${description ?? ''}`);
},
};
}

afterEach(() => {
latestController = undefined;
cleanupFakeDom();
});

describe('useGoalController', () => {
it('clears on Session switch and ignores the previous Session late read', async () => {
const { root } = installReactRenderer();
const first = deferred<GoalState | null>();
let disposers = 0;
const defaults = createFakeGoalServices();
const services = createFakeGoalServices({
goal: {
...defaults.goal,
get: (sessionId) =>
sessionId === 'a' ? first.promise : Promise.resolve(goal('b')),
subscribeChanges: () => () => {
disposers += 1;
},
},
});

await act(async () => renderController(root, services, input('a')));
assert.equal(controller().selectors.indicator, undefined);
await act(async () => renderController(root, services, input('b')));
assert.equal(controller().selectors.indicator?.condition, 'Finish b');
assert.equal(disposers, 1);

await act(async () => first.resolve(goal('a')));
assert.equal(controller().selectors.indicator?.condition, 'Finish b');

await act(async () => root.unmount());
assert.equal(disposers, 2);
});

it('refreshes for matching and broadcast Goal changes only', async () => {
const { root } = installReactRenderer();
let gets = 0;
let emit: ((sessionId: string | undefined) => void) | undefined;
const defaults = createFakeGoalServices();
const services = createFakeGoalServices({
goal: {
...defaults.goal,
get: async () => {
gets += 1;
return gets === 2 ? goal('a', 'achieved') : goal('a');
},
subscribeChanges: (handler) => {
emit = handler;
return () => undefined;
},
},
});

await act(async () => renderController(root, services, input('a')));
assert.equal(gets, 1);
assert.equal(controller().selectors.active, true);

await act(async () => emit?.('other'));
assert.equal(gets, 1);

await act(async () => emit?.('a'));
assert.equal(gets, 2);
assert.equal(controller().selectors.active, false);

await act(async () => emit?.(undefined));
assert.equal(gets, 3);
assert.equal(controller().selectors.active, true);
});

it('snapshots the dialog Session across navigation and routes arm through the port', async () => {
const { root } = installReactRenderer();
const arms: unknown[][] = [];
const defaults = createFakeGoalServices();
const services = createFakeGoalServices({
goal: {
...defaults.goal,
arm: async (...args) => {
arms.push(args);
return { kind: 'armed', goal: goal(args[0]) };
},
},
});

await act(async () => renderController(root, services, input('a')));
await act(async () => controller().commands.openDialog());
await act(async () => renderController(root, services, input('b')));

assert.equal(controller().host.dialogSessionId, 'a');
await act(async () =>
controller().host.arm(controller().host.dialogSessionId!, {
condition: 'ship it',
maxIterations: null,
tokenBudget: 1_000,
}),
);
assert.deepEqual(arms, [
['a', { condition: 'ship it', maxIterations: null, tokenBudget: 1_000 }],
]);
});

it('projects the kill switch and deduplicates pending pause controls', async () => {
const { root } = installReactRenderer();
const pause = deferred<void>();
let pauseCalls = 0;
const errors: string[] = [];
const defaults = createFakeGoalServices();
const services = createFakeGoalServices({
goal: {
...defaults.goal,
get: async () => goal('a'),
pause: () => {
pauseCalls += 1;
return pauseCalls === 1 ? pause.promise : Promise.resolve();
},
},
});

await act(async () => renderController(root, services, input('a', errors)));
const indicator = controller().selectors.indicator;
assert.equal(indicator?.condition, 'Finish a');
assert.equal(indicator?.tokensSpent, 60);
assert.equal(indicator?.tokenBudget, 500);

await act(async () => {
indicator?.onPause?.();
indicator?.onPause?.();
});
assert.equal(pauseCalls, 1);

await act(async () => pause.reject(new Error('offline')));
assert.equal(errors.length, 1);
assert.equal(
errors[0],
'a: Could not pause the goal: The goal may still be continuing. Try again now.',
);

await act(async () => controller().selectors.indicator?.onPause?.());
assert.equal(pauseCalls, 2);
});

it('routes resume and clear controls for paused Goals', async () => {
const { root } = installReactRenderer();
const calls: string[] = [];
const defaults = createFakeGoalServices();
const services = createFakeGoalServices({
goal: {
...defaults.goal,
get: async () => goal('a', 'paused'),
clear: async (sessionId) => {
calls.push(`clear:${sessionId}`);
},
resume: async (sessionId) => {
calls.push(`resume:${sessionId}`);
},
},
});

await act(async () => renderController(root, services, input('a')));
const indicator = controller().selectors.indicator;
assert.equal(indicator?.status, 'paused');
assert.equal(indicator?.pausedAt, 200);
await act(async () => {
indicator?.onResume?.();
indicator?.onClear();
});
assert.deepEqual(calls, ['resume:a', 'clear:a']);
});
});
6 changes: 2 additions & 4 deletions apps/desktop/src/main/__tests__/goal-dialog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { createRoot, type Root } from 'react-dom/client';
import { parseHTML } from 'linkedom';
import { AstryxLocaleProvider, LocaleProvider } from '@maka/ui';
import type { GoalArmOutcome } from '../../shared/goal-arm.js';
import { GoalDialog } from '../../renderer/goal-dialog.js';
import { GoalDialog } from '../../renderer/features/goals/testing.js';

const originalGlobals = {
document: globalThis.document,
Expand Down Expand Up @@ -171,9 +171,6 @@ function installGoalDialog(
cancelAnimationFrame: (handle: number) => clearTimeout(handle),
IS_REACT_ACT_ENVIRONMENT: true,
});
(window as unknown as { maka: unknown }).maka = {
goal: { arm },
};
const container = document.querySelector('#root');
assert.ok(container);
const root = createRoot(container);
Expand All @@ -192,6 +189,7 @@ function installGoalDialog(
children: createElement(AstryxLocaleProvider, {
children: createElement(GoalDialog, {
sessionId,
onArm: arm,
onClose: () => {
closed += 1;
},
Expand Down
65 changes: 65 additions & 0 deletions apps/desktop/src/main/__tests__/goal-model.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { strict as assert } from 'node:assert';
import { describe, it } from 'node:test';
import type { GoalState, GoalStatus } from '@maka/core/goal';
import {
isLiveGoal,
readGoalBudget,
} from '../../renderer/features/goals/testing.js';

function goal(status: GoalStatus, pausedAt?: number): GoalState {
return {
id: 'goal-1',
revision: 1,
sessionId: 'session-1',
condition: 'Finish the refactor',
status,
setAt: 1,
iterations: 2,
maxIterations: 10,
consecutiveNoProgress: 0,
blockCap: 3,
tokensAtStart: 0,
tokensNow: 50,
tokensBaselinePending: false,
...(pausedAt === undefined ? {} : { pausedAt }),
};
}

describe('Goals model', () => {
it('parses only whole, bounded, safe budget values', () => {
assert.deepEqual(readGoalBudget(' ', 1, 10), { kind: 'empty' });
assert.deepEqual(readGoalBudget('7', 1, 10), { kind: 'value', value: 7 });
for (const invalid of ['0', '11', '-1', '1.5', '1e2', '9007199254740992']) {
assert.deepEqual(readGoalBudget(invalid, 1, 10), { kind: 'invalid' });
}
});

it('projects only running and well-formed paused Goals', () => {
assert.equal(isLiveGoal(goal('active')), true);
assert.equal(isLiveGoal(goal('waiting')), true);
assert.equal(isLiveGoal(goal('paused', 12)), true);
assert.equal(isLiveGoal(goal('paused')), false);
assert.equal(isLiveGoal(goal('paused', Number.NaN)), false);
assert.equal(isLiveGoal(goal('achieved')), false);
assert.equal(isLiveGoal(goal('cleared')), false);
});
});
Loading