Files
claude-mem/tests/install-non-tty.test.ts
T
Alex Newman 36b0929fae Server-beta: Postgres storage + independent runtime + BullMQ queue (Phases 1–3) (#2351)
* Add server beta runtime foundation

* Address server beta review findings

* Resolve server beta review comments

* Tighten server beta review follow-ups

* Harden server beta auth and search

* Avoid unnecessary FTS rebuilds

* Block scoped keys from creating projects

* Release BullMQ claims best effort on close

* Address server beta review blockers

* Reset BullMQ claims best effort

* Add Postgres observation storage foundation

* feat(server-beta): add independent runtime service

Introduce src/server/runtime/ as a self-contained server-beta runtime
that owns its lifecycle, Postgres bootstrap, and HTTP boundary without
depending on WorkerService.

ServerBetaService wraps the existing Server class, exposes
/healthz and /v1/info with runtime="server-beta", and persists state
to dedicated paths (.server-beta.pid|.port|.runtime.json). The four
boundary managers (queue, generation worker, provider registry, event
broadcaster) are intentionally disabled in this phase and report their
status through /v1/info; later phases activate them.

Adds plans/2026-05-07-finish-bullmq-branch-ship-plan.md to track the
remaining work for this branch.

Phase 2 of plans/2026-05-07-server-beta-independent-bullmq-observation-runtime.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(server-beta): route CLI lifecycle and bundle separate runtime

scripts/build-hooks.js now produces plugin/scripts/server-beta-service.cjs
as a separate Node CJS bundle, alongside the existing worker-service
bundle. The server-beta runtime is now installable independently.

src/npx-cli/commands/server.ts routes start|stop|restart|status to the
server-beta lifecycle instead of the legacy worker. The worker keeps its
own start|stop|restart|status under the worker namespace; the two
runtimes can be operated independently.

src/services/worker-service.ts adds a server-* command parser branch
that delegates to the sibling server-beta-service.cjs bundle so
direct worker-service invocations still route to the right runtime.

tests/npx-cli-server-namespace.test.ts updated to expect server-beta
lifecycle routing.

Includes rebuilt plugin/scripts/*.cjs bundles produced by
build-and-sync.

Phase 2 of plans/2026-05-07-server-beta-independent-bullmq-observation-runtime.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(server-beta): add BullMQ job queue primitives

Introduce src/server/jobs/ as the queue-side primitives that Phase 3 of
the server-beta runtime needs to operate.

types.ts defines a discriminated union over the four job kinds (event,
event-batch, summary, reindex) and maps each to a per-kind BullMQ queue
name and deterministic-ID prefix.

job-id.ts builds deterministic, colon-free BullMQ jobIds from
(kind, team, project, source). The colon ban exists because BullMQ uses
':' as a Redis key separator internally; embedding ':' in jobIds
breaks scan and state lookups.

ServerJobQueue.ts is a thin wrapper over BullMQ Queue + Worker that
enforces autorun:false, default concurrency 1, and an attached error
listener — all per BullMQ docs requirements. Test seams accept queue
and worker factories so unit tests do not need Redis.

outbox.ts publishes through the Postgres ObservationGenerationJob
repository as canonical history. enqueueOutbox writes the row first,
then publishes to BullMQ; if BullMQ throws, the row is transitioned to
failed and a failed event is appended. reconcileOnStartup re-enqueues
queued + processing rows after a restart, replacing terminal BullMQ
jobs that may still be holding the deterministic ID slot. markCompleted
and markFailed wrap transitionStatus and append the matching event row.

Includes 20 unit tests covering deterministic ID stability, colon-free
output, queue lifecycle, error-listener attachment, double-start
refusal, idempotent enqueue, BullMQ failure rollback, startup
reconciliation, max-attempts skipping, and completion / failure /
retry transitions.

Phase 3 commit 1 of plans/2026-05-07-server-beta-independent-bullmq-observation-runtime.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(server-beta): activate queue boundary in runtime service

Wire ActiveServerBetaQueueManager into the server-beta runtime graph.
The active manager owns one ServerJobQueue per generation kind (event,
event-batch, summary, reindex) and surfaces lane metadata through
boundary health.

Selection is opt-in and fail-fast: if CLAUDE_MEM_QUEUE_ENGINE is set to
bullmq the active manager is constructed (and any Redis/config error
throws — no silent fallback to SQLite, per Phase 3 anti-pattern guard).
For any other engine the disabled boundary remains so worker-era and
test setups stay compatible.

Widens ServerBetaBoundaryHealth.status to a discriminated union
('disabled' | 'active' | 'errored') with optional details. The disabled
adapter still emits status='disabled', which keeps the existing
server-beta-service test green.

ServerBetaService receives the manager through a new optional
queueManager field on CreateServerBetaServiceOptions so test graphs
and Phase 4 wiring can inject custom managers.

Adds tests/server/runtime/active-queue-manager.test.ts covering bullmq
guard, active health shape, per-kind queue access, close behavior, and
post-close errored health.

Phase 3 commit 2 of plans/2026-05-07-server-beta-independent-bullmq-observation-runtime.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(server-beta): cap /v1/events/batch at 500 events

Prevents unbounded array DoS surface flagged in PR review.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 01:20:07 -07:00

287 lines
12 KiB
TypeScript

import { describe, it, expect } from 'bun:test';
import { readFileSync } from 'fs';
import { join } from 'path';
const installSourcePath = join(
__dirname,
'..',
'src',
'npx-cli',
'commands',
'install.ts',
);
const installSource = readFileSync(installSourcePath, 'utf-8');
const codexInstallerSourcePath = join(
__dirname,
'..',
'src',
'services',
'integrations',
'CodexCliInstaller.ts',
);
const codexInstallerSource = readFileSync(codexInstallerSourcePath, 'utf-8');
const syncMarketplaceSourcePath = join(
__dirname,
'..',
'scripts',
'sync-marketplace.cjs',
);
const syncMarketplaceSource = readFileSync(syncMarketplaceSourcePath, 'utf-8');
const transcriptConfigSourcePath = join(
__dirname,
'..',
'src',
'services',
'transcripts',
'config.ts',
);
const transcriptConfigSource = readFileSync(transcriptConfigSourcePath, 'utf-8');
describe('Install Non-TTY Support', () => {
describe('isInteractive flag', () => {
it('defines isInteractive based on process.stdin.isTTY', () => {
expect(installSource).toContain('const isInteractive = process.stdin.isTTY === true');
});
it('uses strict equality (===) not truthy check for isTTY', () => {
const match = installSource.match(/const isInteractive = process\.stdin\.isTTY === true/);
expect(match).not.toBeNull();
});
});
describe('runTasks helper', () => {
it('defines a runTasks function', () => {
expect(installSource).toContain('async function runTasks');
});
it('has interactive branch using p.tasks', () => {
expect(installSource).toContain('await p.tasks(tasks)');
});
it('has non-interactive fallback using console.log', () => {
expect(installSource).toContain('console.log(` ${msg}`)');
});
it('branches on isInteractive', () => {
expect(installSource).toContain('if (isInteractive)');
});
});
describe('log wrapper', () => {
it('defines log.info that falls back to console.log', () => {
expect(installSource).toContain('info: (msg: string) =>');
expect(installSource).toMatch(/info:.*console\.log/);
});
it('defines log.success that falls back to console.log', () => {
expect(installSource).toContain('success: (msg: string) =>');
expect(installSource).toMatch(/success:.*console\.log/);
});
it('defines log.warn that falls back to console.warn', () => {
expect(installSource).toContain('warn: (msg: string) =>');
expect(installSource).toMatch(/warn:.*console\.warn/);
});
it('defines log.error that falls back to console.error', () => {
expect(installSource).toContain('error: (msg: string) =>');
expect(installSource).toMatch(/error:.*console\.error/);
});
});
describe('non-interactive install path', () => {
it('defaults to claude-code when not interactive and no IDE specified', () => {
expect(installSource).toContain("selectedIDEs = ['claude-code']");
});
it('uses console.log for intro in non-interactive mode', () => {
expect(installSource).toContain("console.log('claude-mem install')");
});
it('uses console.log for note/summary in non-interactive mode', () => {
expect(installSource).toContain("console.log(`\\n ${installStatus}`)");
});
it('copies Codex marketplace metadata to the durable marketplace directory', () => {
const copyRegion = installSource.slice(
installSource.indexOf('const allowedTopLevelEntries = ['),
installSource.indexOf('function copyPluginToCache'),
);
expect(copyRegion).toContain("'.agents'");
expect(copyRegion).toContain("'.codex-plugin'");
expect(copyRegion).toContain("'.mcp.json'");
});
it('validates the bundled plugin as the Codex marketplace source', () => {
expect(codexInstallerSource).toContain("path.join('plugin', '.codex-plugin', 'plugin.json')");
expect(codexInstallerSource).toContain("path.join('plugin', '.mcp.json')");
expect(codexInstallerSource).toContain("path.join('plugin', 'hooks', 'codex-hooks.json')");
expect(codexInstallerSource).toContain("path.join('plugin', 'skills', 'mem-search', 'SKILL.md')");
});
it('does not exclude MCP manifests during local marketplace sync', () => {
const gitignoreExcludeRegion = syncMarketplaceSource.slice(
syncMarketplaceSource.indexOf('function getGitignoreExcludes'),
syncMarketplaceSource.indexOf('const branch = getCurrentBranch'),
);
expect(gitignoreExcludeRegion).toContain("'.mcp.json'");
expect(gitignoreExcludeRegion).toContain('syncManagedFiles.has(line)');
});
it('registers Codex against the durable marketplace directory', () => {
expect(installSource).toContain('installCodexCli(marketplaceDirectory())');
});
it('refreshes Codex marketplace cache after registration', () => {
const installRegion = codexInstallerSource.slice(
codexInstallerSource.indexOf('export async function installCodexCli'),
codexInstallerSource.indexOf('export function uninstallCodexCli'),
);
expect(installRegion).toContain("['plugin', 'marketplace', 'upgrade', MARKETPLACE_NAME]");
expect(installRegion).toContain('installed plugin cache');
});
it('replaces stale Codex marketplace registrations from a different source', () => {
const registerRegion = codexInstallerSource.slice(
codexInstallerSource.indexOf('function registerCodexMarketplace'),
codexInstallerSource.indexOf('function parseSemver'),
);
expect(registerRegion).toContain('isMarketplaceDifferentSourceError(error)');
expect(registerRegion).toContain("['plugin', 'marketplace', 'remove', MARKETPLACE_NAME]");
expect(registerRegion).toContain("['plugin', 'marketplace', 'add', marketplaceRoot]");
});
it('enables Codex plugin hooks during install', () => {
const installRegion = codexInstallerSource.slice(
codexInstallerSource.indexOf('export async function installCodexCli'),
codexInstallerSource.indexOf('export function uninstallCodexCli'),
);
expect(installRegion).toContain("['features', 'enable', 'plugin_hooks']");
expect(installRegion).toContain('codex features enable plugin_hooks');
});
it('captures Codex CLI output for install failure reporting', () => {
const runCodexRegion = codexInstallerSource.slice(
codexInstallerSource.indexOf('function runCodex'),
codexInstallerSource.indexOf('function removeCodexAgentsMdContext'),
);
expect(runCodexRegion).toContain('spawnSync');
expect(runCodexRegion).not.toContain("stdio: 'inherit'");
});
it('checks Codex CLI marketplace version before registration', () => {
const installRegion = codexInstallerSource.slice(
codexInstallerSource.indexOf('export async function installCodexCli'),
codexInstallerSource.indexOf('export function uninstallCodexCli'),
);
expect(codexInstallerSource).toContain("const MIN_CODEX_MARKETPLACE_VERSION = '0.128.0'");
expect(codexInstallerSource).toContain("spawnSync('codex', ['--version']");
expect(installRegion.indexOf('assertCodexMarketplaceSupported()'))
.toBeLessThan(installRegion.indexOf('registerCodexMarketplace(marketplaceRoot)'));
});
it('removes legacy Codex AGENTS context only after marketplace registration succeeds', () => {
const installRegion = codexInstallerSource.slice(
codexInstallerSource.indexOf('export async function installCodexCli'),
codexInstallerSource.indexOf('export function uninstallCodexCli'),
);
expect(installRegion.indexOf('registerCodexMarketplace(marketplaceRoot)'))
.toBeLessThan(installRegion.indexOf('cleanupLegacyCodexAgentsMdContext()'));
});
it('reports legacy Codex AGENTS cleanup failures to callers', () => {
expect(codexInstallerSource).toContain('function removeCodexAgentsMdContext(): boolean');
expect(codexInstallerSource).toContain('function disableCodexTranscriptAgentsContext(): boolean');
expect(codexInstallerSource).toContain('if (!cleanupLegacyCodexAgentsMdContext())');
expect(codexInstallerSource).toContain('if (!cleanupLegacyCodexTranscriptAgentsContext())');
});
it('does not fail Codex install after marketplace registration when only AGENTS cleanup fails', () => {
const installRegion = codexInstallerSource.slice(
codexInstallerSource.indexOf('export async function installCodexCli'),
codexInstallerSource.indexOf('export function uninstallCodexCli'),
);
const cleanupFailureRegion = installRegion.slice(
installRegion.indexOf('if (!cleanupLegacyCodexAgentsMdContext())'),
installRegion.indexOf('Installation complete!'),
);
expect(cleanupFailureRegion).toContain('console.warn');
expect(cleanupFailureRegion).not.toContain('return 1');
});
it('does not seed new Codex transcript watcher configs with AGENTS context injection', () => {
expect(transcriptConfigSource).toContain("name: 'codex'");
const codexWatchRegion = transcriptConfigSource.slice(
transcriptConfigSource.indexOf("name: 'codex'"),
transcriptConfigSource.indexOf('stateFile: DEFAULT_STATE_PATH'),
);
expect(codexWatchRegion).toContain("path: '~/.codex/sessions/**/*.jsonl'");
expect(codexWatchRegion).not.toContain("mode: 'agents'");
expect(codexWatchRegion).not.toContain('updateOn');
});
});
describe('TaskDescriptor interface', () => {
it('defines a task interface with title and task function', () => {
expect(installSource).toContain('interface TaskDescriptor');
expect(installSource).toContain('title: string');
expect(installSource).toContain('task: (message: (msg: string) => void) => Promise<string>');
});
});
describe('InstallOptions interface', () => {
it('exports InstallOptions with optional ide field', () => {
expect(installSource).toContain('export interface InstallOptions');
expect(installSource).toContain('ide?: string');
});
});
describe('runtime selection', () => {
it('offers Server (beta) while keeping worker as the default runtime', () => {
expect(installSource).toContain("'server-beta'");
expect(installSource).toContain('Server (beta)');
expect(installSource).toContain("initialValue: 'worker'");
expect(installSource).toContain('CLAUDE_MEM_RUNTIME');
});
});
describe('post-install Next Steps copy', () => {
it('frames the choice as two paths', () => {
expect(installSource).toContain('Two paths from here:');
});
it('sets timing honesty about second-session memory injection', () => {
expect(installSource).toContain('Memory injection starts on your second session in a project.');
});
it('addresses privacy: everything stays local', () => {
expect(installSource).toContain('Everything stays in ');
expect(installSource).toContain("pc.cyan('~/.claude-mem')");
});
it('keeps /learn-codebase as the optional front-load path', () => {
expect(installSource).toContain('/learn-codebase');
});
it('demotes the uninstall caveat into a dim footer', () => {
expect(installSource).toContain('close all Claude Code sessions before uninstalling');
});
it('does not advertise /mem-search in the post-install Next Steps', () => {
const nextStepsRegion = installSource.slice(
installSource.indexOf('const nextSteps = '),
installSource.indexOf("p.note(nextSteps.join"),
);
expect(nextStepsRegion).not.toContain('/mem-search');
});
it('does not advertise /knowledge-agent in the post-install Next Steps', () => {
const nextStepsRegion = installSource.slice(
installSource.indexOf('const nextSteps = '),
installSource.indexOf("p.note(nextSteps.join"),
);
expect(nextStepsRegion).not.toContain('/knowledge-agent');
});
});
});