Files
open-multi-agent/packages/core/tests/task-utils.test.ts
T
Jack Chen 1d37ce6026 refactor(repo): monorepo groundwork for an easier first run (#303)
* refactor(repo): monorepo groundwork for an easier first run

Trying OMA for the first time costs more than it should: a lot to read before anything runs, and no one-command way to watch a team actually work. The plan is to fix that with a `npm create oma` scaffold (a multi-agent demo that runs on the first try) and an npm README that leads with getting started. Neither lands cleanly until core lives in its own package, so this PR does that part first.

This is groundwork, not the payoff: nothing changes for anyone installing `@open-multi-agent/core` today. It turns the repo into an npm-workspaces monorepo with core in `packages/core/`, and splits the README into a GitHub landing page (root) and the npm package page (`packages/core/`). Doing it now, while the repo is small, avoids migrating twice.

The published package is byte-identical: same name, same three runtime dependencies, same entry points, guarded by an isolated-install smoke test. `npm ci` + lint + test (1060) + build pass; the tarball still ships only `dist/`, `README.md`, `LICENSE`, and `package.json`.

* build: restore cross-platform optional deps in package-lock.json

Regenerating the lockfile on macOS pruned the non-host rollup/esbuild optional binaries (npm/cli#4828), so Linux CI could not load @rollup/rollup-linux-x64-gnu and vitest failed to start. Rebuild the lock from the pre-migration base so every platform variant is present again. Verified with npm ci + lint + build + test (1060).

* fix(examples,docs): correct monorepo paths and anchors after the packages/core move

with-vercel-ai-sdk example (pre-existing, surfaced by the move):
- package.json: the @open-multi-agent/core file: link was file:../../, which
  resolves to packages/core/examples (no manifest). Corrected to file:../../..
  (packages/core); predev cd updated to match.
- package-lock.json regenerated: was linking ../.. and pinning a stale core
  1.4.0; now links ../../.. at 1.7.0.
- README: setup paths and the file: note updated.

docs missed by the move:
- docs/cli.md: repo-clone build output is now packages/core/dist/cli/oma.js;
  the quick-start anchor now lives in the package README.
- CONTRIBUTING.md: the architecture anchor and src/ entry-point paths now live
  under packages/core/.
2026-06-18 17:31:52 +08:00

156 lines
5.4 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import {
createTask,
isTaskReady,
getTaskDependencyOrder,
validateTaskDependencies,
} from '../src/task/task.js'
import type { Task } from '../src/types.js'
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function task(id: string, opts: { dependsOn?: string[]; status?: Task['status'] } = {}): Task {
const t = createTask({ title: id, description: `task ${id}` })
return { ...t, id, dependsOn: opts.dependsOn, status: opts.status ?? 'pending' }
}
// ---------------------------------------------------------------------------
// createTask
// ---------------------------------------------------------------------------
describe('createTask', () => {
it('creates a task with pending status and timestamps', () => {
const t = createTask({ title: 'Test', description: 'A test task' })
expect(t.id).toBeDefined()
expect(t.status).toBe('pending')
expect(t.createdAt).toBeInstanceOf(Date)
expect(t.updatedAt).toBeInstanceOf(Date)
})
it('copies dependsOn array (no shared reference)', () => {
const deps = ['a']
const t = createTask({ title: 'T', description: 'D', dependsOn: deps })
deps.push('b')
expect(t.dependsOn).toEqual(['a'])
})
})
// ---------------------------------------------------------------------------
// isTaskReady
// ---------------------------------------------------------------------------
describe('isTaskReady', () => {
it('returns true for a pending task with no dependencies', () => {
const t = task('a')
expect(isTaskReady(t, [t])).toBe(true)
})
it('returns false for a non-pending task', () => {
const t = task('a', { status: 'blocked' })
expect(isTaskReady(t, [t])).toBe(false)
})
it('returns true when all dependencies are completed', () => {
const dep = task('dep', { status: 'completed' })
const t = task('a', { dependsOn: ['dep'] })
expect(isTaskReady(t, [dep, t])).toBe(true)
})
it('returns false when a dependency is not yet completed', () => {
const dep = task('dep', { status: 'in_progress' })
const t = task('a', { dependsOn: ['dep'] })
expect(isTaskReady(t, [dep, t])).toBe(false)
})
it('returns false when a dependency is missing from the task set', () => {
const t = task('a', { dependsOn: ['ghost'] })
expect(isTaskReady(t, [t])).toBe(false)
})
})
// ---------------------------------------------------------------------------
// getTaskDependencyOrder
// ---------------------------------------------------------------------------
describe('getTaskDependencyOrder', () => {
it('returns empty array for empty input', () => {
expect(getTaskDependencyOrder([])).toEqual([])
})
it('returns tasks with no deps first', () => {
const a = task('a')
const b = task('b', { dependsOn: ['a'] })
const ordered = getTaskDependencyOrder([b, a])
expect(ordered[0].id).toBe('a')
expect(ordered[1].id).toBe('b')
})
it('handles a diamond dependency (a → b,c → d)', () => {
const a = task('a')
const b = task('b', { dependsOn: ['a'] })
const c = task('c', { dependsOn: ['a'] })
const d = task('d', { dependsOn: ['b', 'c'] })
const ordered = getTaskDependencyOrder([d, c, b, a])
const ids = ordered.map((t) => t.id)
// a must come before b and c; b and c must come before d
expect(ids.indexOf('a')).toBeLessThan(ids.indexOf('b'))
expect(ids.indexOf('a')).toBeLessThan(ids.indexOf('c'))
expect(ids.indexOf('b')).toBeLessThan(ids.indexOf('d'))
expect(ids.indexOf('c')).toBeLessThan(ids.indexOf('d'))
})
it('returns partial result when a cycle exists', () => {
const a = task('a', { dependsOn: ['b'] })
const b = task('b', { dependsOn: ['a'] })
const ordered = getTaskDependencyOrder([a, b])
// Neither can be ordered — result should be empty (or partial)
expect(ordered.length).toBeLessThan(2)
})
})
// ---------------------------------------------------------------------------
// validateTaskDependencies
// ---------------------------------------------------------------------------
describe('validateTaskDependencies', () => {
it('returns valid for tasks with no deps', () => {
const result = validateTaskDependencies([task('a'), task('b')])
expect(result.valid).toBe(true)
expect(result.errors).toHaveLength(0)
})
it('detects self-dependency', () => {
const t = task('a', { dependsOn: ['a'] })
const result = validateTaskDependencies([t])
expect(result.valid).toBe(false)
expect(result.errors[0]).toContain('depends on itself')
})
it('detects unknown dependency', () => {
const t = task('a', { dependsOn: ['ghost'] })
const result = validateTaskDependencies([t])
expect(result.valid).toBe(false)
expect(result.errors[0]).toContain('unknown dependency')
})
it('detects a cycle (a → b → a)', () => {
const a = task('a', { dependsOn: ['b'] })
const b = task('b', { dependsOn: ['a'] })
const result = validateTaskDependencies([a, b])
expect(result.valid).toBe(false)
expect(result.errors.some((e) => e.toLowerCase().includes('cyclic'))).toBe(true)
})
it('detects a longer cycle (a → b → c → a)', () => {
const a = task('a', { dependsOn: ['c'] })
const b = task('b', { dependsOn: ['a'] })
const c = task('c', { dependsOn: ['b'] })
const result = validateTaskDependencies([a, b, c])
expect(result.valid).toBe(false)
})
})