Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { afterEach, describe, it } from 'node:test';
import {
readSessionListViewMode,
writeSessionListViewMode,
} from '../../renderer/session-list-layout.js';
} from '../../renderer/features/session-navigation/testing.js';

const VIEW_MODE_KEY = 'maka-chat-list-view-mode-v1';

Expand Down
125 changes: 125 additions & 0 deletions apps/desktop/src/main/__tests__/session-navigation-boundary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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 { readdirSync, readFileSync } from 'node:fs';
import { join, relative, resolve } from 'node:path';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';

const desktopRoot = resolve(fileURLToPath(new URL('../../../', import.meta.url)));
const featureRoot = join(
desktopRoot,
'src',
'renderer',
'features',
'session-navigation',
);

function sourceFiles(root: string): string[] {
return readdirSync(root, { withFileTypes: true }).flatMap((entry) => {
const path = join(root, entry.name);
if (entry.isDirectory()) return sourceFiles(path);
return /\.(?:ts|tsx|md)$/.test(entry.name) ? [path] : [];
});
}

describe('Session Navigation feature boundary', () => {
it('contains no Desktop global bridge or shell/process imports', () => {
const violations: string[] = [];
for (const path of sourceFiles(featureRoot)) {
const source = readFileSync(path, 'utf8');
const name = relative(desktopRoot, path);
if (source.includes('window.maka')) violations.push(`${name}: Desktop global`);
for (const match of source.matchAll(/from\s+['"]([^'"]+)['"]/g)) {
const imported = match[1] ?? '';
if (
imported.includes('app-shell') ||
imported.includes('/preload/') ||
imported.includes('/main/')
) {
violations.push(`${name}: ${imported}`);
}
}
}
assert.deepEqual(violations, []);
});

it('is consumed outside the feature only through public entries', () => {
const allowed = /\/features\/session-navigation\/(?:index|testing)(?:\.js)?$/;
const violations: string[] = [];
for (const root of [join(desktopRoot, 'src'), join(desktopRoot, 'stories')]) {
for (const path of sourceFiles(root)) {
if (path.startsWith(featureRoot)) continue;
const source = readFileSync(path, 'utf8');
for (const match of source.matchAll(
/from\s+['"]([^'"]*features\/session-navigation[^'"]*)['"]/g,
)) {
const imported = match[1] ?? '';
const normalized = imported.replace(/\\/g, '/');
const explicitEntry = normalized.endsWith('/features/session-navigation')
? `${normalized}/index`
: normalized;
if (!allowed.test(explicitEntry)) {
violations.push(`${relative(desktopRoot, path)}: ${imported}`);
}
}
}
}
assert.deepEqual(violations, []);
});

it('keeps fake services out of the production entry', () => {
const productionEntry = readFileSync(join(featureRoot, 'index.ts'), 'utf8');
assert.equal(
productionEntry.includes('createFakeSessionNavigationServices'),
false,
);
assert.equal(productionEntry.includes("from './testing"), false);
});

it('keeps rail projection, persistence, and row mutation ownership out of AppShell', () => {
const appShell = readFileSync(
join(desktopRoot, 'src', 'renderer', 'app-shell.tsx'),
'utf8',
);
for (const forbidden of [
'<SessionListPanel',
'deriveSessionRail(',
'deriveSessionRevisionNavigation(',
'readSessionListViewMode(',
'sessionRowActionRegistry',
'window.maka.sessions.setFlagged',
'window.maka.sessions.archive',
'window.maka.sessions.unarchive',
'window.maka.sessions.rename',
'window.maka.sessions.remove',
]) {
assert.equal(appShell.includes(forbidden), false, forbidden);
}
assert.equal(
appShell.includes('const sessionNavigation = useSessionNavigationController({'),
true,
);
assert.equal(
appShell.includes('<SessionNavigationHost'),
true,
);
});
});
196 changes: 196 additions & 0 deletions apps/desktop/src/main/__tests__/session-navigation-controller.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* 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 { ProjectRecord } from '@maka/core/project';
import { LocaleProvider } from '@maka/ui';
import { cleanupFakeDom, installReactRenderer } from './fake-dom.js';
import {
createFakeSessionNavigationServices,
SessionNavigationServicesProvider,
useSessionNavigationController,
type SessionNavigationController,
type SessionNavigationSession,
type UseSessionNavigationControllerInput,
} from '../../renderer/features/session-navigation/testing.js';

function session(
id: string,
overrides: Partial<SessionNavigationSession> = {},
): SessionNavigationSession {
return {
id,
name: id,
isFlagged: false,
isArchived: false,
labels: [],
hasUnread: false,
status: 'active',
backend: 'fake',
llmConnectionSlug: 'test',
connectionLocked: true,
model: 'test',
permissionMode: 'ask',
profileId: 'local',
profileName: 'Local',
profileKind: 'local',
...overrides,
};
}

const project: ProjectRecord = {
id: 'project',
name: 'Project',
locations: [{ path: '/repo', isWorktree: false }],
available: true,
};

let latestController: SessionNavigationController | undefined;

function ControllerProbe(props: UseSessionNavigationControllerInput) {
latestController = useSessionNavigationController(props);
return null;
}

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

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

function input(
sessions: SessionNavigationSession[],
activeSessionId: string | undefined,
calls: string[] = [],
targets: unknown[] = [],
): UseSessionNavigationControllerInput {
return {
sessions,
activeSessionId,
hiddenSessionIds: new Set(['hidden']),
projects: [project],
activateSession: (sessionId) => calls.push(`activate:${sessionId ?? 'none'}`),
clearActiveMessages: () => calls.push('clear-messages'),
clearSessionRendererState: (sessionId) => calls.push(`clear:${sessionId}`),
exitWorkHub: () => calls.push('exit-workhub'),
refreshSessions: async () => sessions,
selectSessionSurface: () => calls.push('select-sessions'),
setSearchTarget: (target) => targets.push(target),
toastApi: {
success: () => undefined,
error: () => undefined,
confirm: async () => true,
},
};
}

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

describe('useSessionNavigationController', () => {
it('projects linked, archived, hidden, Project, and Runtime Host Sessions once', async () => {
const { root } = installReactRenderer();
const sessions = [
session('root', { projectId: 'project', cwd: '/repo' }),
session('child', {
parentSessionId: 'root',
subagentParent: {
kind: 'subagent',
parentSessionId: 'root',
spawnedBy: {
parentRunId: 'run',
parentTurnId: 'turn',
toolCallId: 'tool',
},
lifecycle: 'foreground',
},
}),
session('remote', {
profileId: 'remote-profile',
profileName: 'Remote Mac',
profileKind: 'remote',
}),
session('archived', { isArchived: true }),
session('hidden'),
];

await act(async () => renderController(root, input(sessions, 'child')));

assert.deepEqual(
controller().selectors.visibleSessions.map(({ id }) => id),
['root', 'remote'],
);
assert.equal(controller().selectors.activeRowId, 'root');
assert.equal(controller().selectors.activeParentSession?.id, 'root');
assert.deepEqual(controller().selectors.branchBanner, {
parentSessionId: 'root',
parentSessionName: 'root',
});
assert.deepEqual(
controller().selectors.groups.map(({ id }) => id),
['project:project', 'runtime-host:remote-profile'],
);
assert.equal(controller().selectors.sessionMeta(sessions[2]!), 'Remote Mac');
});

it('owns Session jumps and preserves turn-target clearing semantics', async () => {
const { root } = installReactRenderer();
const calls: string[] = [];
const targets: unknown[] = [];
const sessions = [session('a')];
await act(async () => renderController(root, input(sessions, undefined, calls, targets)));

await act(async () => controller().commands.openSession('a', 'turn-2', 9));
await act(async () => controller().commands.openSession('a'));

assert.deepEqual(calls, [
'exit-workhub',
'select-sessions',
'activate:a',
'exit-workhub',
'select-sessions',
'activate:a',
]);
assert.equal(typeof (targets[0] as { nonce: unknown }).nonce, 'number');
assert.deepEqual(
{ ...(targets[0] as Record<string, unknown>), nonce: 0 },
{ sessionId: 'a', turnId: 'turn-2', sequence: 9, nonce: 0 },
);
assert.equal(targets[1], null);
});
});
Loading