File size: 30,230 Bytes
4e23b01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 | import { chmod, mkdir, mkdtemp, realpath, rm, symlink, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join, sep } from 'node:path';
import { IModelCatalog, IWorkspaceInstanceManager } from '@moonshot-ai/agent-core-v2';
import { HostFileSystem } from '@moonshot-ai/agent-core-v2/os/backends/node-local/hostFsService';
import { FakeRuntime } from '@moonshot-ai/agent-core-v2/runtime/fakeRuntime';
import { ErrorCode } from '../src/protocol/error-codes';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { type RunningServer, startServer } from '../src/start';
import { TEST_HOST_IDENTITY } from './helpers/hostIdentity';
import { authHeaders } from './helpers/auth';
import { fakeModelCatalog } from './helpers/fakeModelCatalog';
interface Envelope<T> {
code: number;
msg: string;
data: T;
request_id: string;
details?: { path: string; message: string }[];
}
interface FsEntryWire {
path: string;
name: string;
kind: string;
size?: number;
modified_at: string;
etag?: string;
mime?: string;
}
describe('server-v2 /api/v1 fs routes', () => {
let server: RunningServer | undefined;
let home: string | undefined;
let work: string | undefined;
let base: string;
beforeAll(async () => {
home = await mkdtemp(join(tmpdir(), 'kimi-server-v2-fs-home-'));
server = await startServer({
hostIdentity: TEST_HOST_IDENTITY,
host: '127.0.0.1',
port: 0,
homeDir: home,
logLevel: 'silent',
seeds: [[IModelCatalog, fakeModelCatalog()]],
});
base = `http://127.0.0.1:${server.port}`;
});
beforeEach(async () => {
work = await mkdtemp(join(tmpdir(), 'kimi-server-v2-fs-work-'));
});
afterEach(async () => {
if (work !== undefined) {
await rm(work, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 });
work = undefined;
}
});
afterAll(async () => {
if (server !== undefined) {
await server.close();
server = undefined;
}
if (home !== undefined) {
await rm(home, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 });
home = undefined;
}
});
async function createSession(): Promise<string> {
const res = await fetch(`${base}/api/v1/sessions`, {
method: 'POST',
headers: authHeaders(server as RunningServer, { 'content-type': 'application/json' }),
body: JSON.stringify({ metadata: { cwd: work as string } }),
} as never);
const body = (await res.json()) as Envelope<{ id: string }>;
expect(body.code).toBe(0);
return body.data.id;
}
async function postFs<T>(id: string, action: string, body: unknown, runtimeId = 'local'): Promise<Envelope<T>> {
const res = await fetch(`${base}/api/v1/sessions/${id}/fs:${action}`, {
method: 'POST',
headers: authHeaders(server as RunningServer, { 'content-type': 'application/json' }),
body: JSON.stringify({ runtime_id: runtimeId, ...(body as object) }),
} as never);
return (await res.json()) as Envelope<T>;
}
it('defaults fs actions to the local runtime when runtime_id is omitted', async () => {
await writeFile(join(work!, 'a.txt'), 'hello');
const id = await createSession();
const res = await fetch(`${base}/api/v1/sessions/${id}/fs:stat`, {
method: 'POST',
headers: authHeaders(server as RunningServer, { 'content-type': 'application/json' }),
body: JSON.stringify({ path: 'a.txt' }),
} as never);
const body = (await res.json()) as Envelope<FsEntryWire>;
expect(body.code).toBe(0);
expect(body.data.name).toBe('a.txt');
});
it('fs:stat returns a file entry with the protocol shape', async () => {
await writeFile(join(work!, 'a.txt'), 'hello');
const id = await createSession();
const body = await postFs<FsEntryWire>(id, 'stat', { path: 'a.txt' });
expect(body.code).toBe(0);
expect(body.data.name).toBe('a.txt');
expect(body.data.kind).toBe('file');
expect(body.data.size).toBe(5);
expect(typeof body.data.modified_at).toBe('string');
expect(typeof body.data.etag).toBe('string');
});
it('fs:stat maps a missing path to FS_PATH_NOT_FOUND', async () => {
const id = await createSession();
const body = await postFs<null>(id, 'stat', { path: 'nope.txt' });
expect(body.code).toBe(ErrorCode.FS_PATH_NOT_FOUND);
});
it('fs:read returns utf-8 content', async () => {
await writeFile(join(work!, 'a.txt'), 'hello world');
const id = await createSession();
const body = await postFs<{ content: string; encoding: string; size: number }>(
id,
'read',
{ path: 'a.txt' },
);
expect(body.code).toBe(0);
expect(body.data.content).toBe('hello world');
expect(body.data.encoding).toBe('utf-8');
expect(body.data.size).toBe(11);
});
it('fs:read uses the selected non-local runtime and mapped workspace root', async () => {
await writeFile(join(work!, 'selected.txt'), 'local');
const remote = await mkdtemp(join(tmpdir(), 'kimi-server-v2-fs-remote-'));
await writeFile(join(remote, 'selected.txt'), 'remote');
const id = await createSession();
const provider = await server!.core.accessor.get(IWorkspaceInstanceManager).addProvider({
id: 'remote-test-provider',
imports: { root: [], imports: [], local: [] },
attach: async (context, host) => {
const runtime = Object.assign(
new FakeRuntime(
{ workspaceId: context.id, runtimeId: 'remote-test', generation: 'remote-generation' },
{
capabilities: ['fs'],
mapWorkspaceRoots: () => ({ workDir: remote, additionalDirs: [] }),
},
),
{ fs: new HostFileSystem() },
);
const registration = host.registerRuntime(runtime);
return { dispose: () => registration.remove() };
},
});
try {
const body = await postFs<{ content: string }>(
id,
'read',
{ path: 'selected.txt' },
'remote-test',
);
expect(body.code).toBe(0);
expect(body.data.content).toBe('remote');
} finally {
await provider.dispose();
await rm(remote, { recursive: true, force: true });
}
});
it('fs:read maps a directory to FS_IS_DIRECTORY', async () => {
const id = await createSession();
const body = await postFs<null>(id, 'read', { path: '.' });
expect(body.code).toBe(ErrorCode.FS_IS_DIRECTORY);
});
it('fs:read maps a permission-denied host error to FS_PERMISSION_DENIED', async () => {
if (process.getuid?.() === 0) return;
const file = join(work!, 'locked.txt');
await writeFile(file, 'secret');
await chmod(file, 0o000);
try {
const id = await createSession();
const body = await postFs<null>(id, 'read', { path: 'locked.txt' });
expect(body.code).toBe(ErrorCode.FS_PERMISSION_DENIED);
} finally {
await chmod(file, 0o644);
}
});
it('fs:list returns items', async () => {
await writeFile(join(work!, 'a.txt'), '');
await writeFile(join(work!, 'b.txt'), '');
const id = await createSession();
const body = await postFs<{ items: FsEntryWire[]; truncated: boolean }>(id, 'list', {});
expect(body.code).toBe(0);
const names = body.data.items.map((i) => i.name).sort();
expect(names).toEqual(['a.txt', 'b.txt']);
expect(body.data.truncated).toBe(false);
});
it('fs:mkdir creates a directory and rejects duplicates', async () => {
const id = await createSession();
const created = await postFs<FsEntryWire>(id, 'mkdir', { path: 'sub' });
expect(created.code).toBe(0);
expect(created.data.kind).toBe('directory');
const dup = await postFs<null>(id, 'mkdir', { path: 'sub' });
expect(dup.code).toBe(ErrorCode.FS_ALREADY_EXISTS);
});
it('fs:stat_many returns null for missing paths', async () => {
await writeFile(join(work!, 'a.txt'), 'hi');
const id = await createSession();
const body = await postFs<{ entries: Record<string, FsEntryWire | null> }>(
id,
'stat_many',
{ paths: ['a.txt', 'missing.txt'] },
);
expect(body.code).toBe(0);
expect(body.data.entries['a.txt']?.kind).toBe('file');
expect(body.data.entries['missing.txt']).toBeNull();
});
it('fs:search finds files by query', async () => {
await writeFile(join(work!, 'alpha.ts'), '');
await writeFile(join(work!, 'beta.ts'), '');
const id = await createSession();
const body = await postFs<{ items: { path: string }[]; truncated: boolean }>(
id,
'search',
{ query: 'alpha' },
);
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain('alpha.ts');
});
it('fs:search resolves a registered workspace id when no session exists', async () => {
await writeFile(join(work!, 'gamma.ts'), '');
const res = await fetch(`${base}/api/v1/workspaces`, {
method: 'POST',
headers: authHeaders(server as RunningServer, { 'content-type': 'application/json' }),
body: JSON.stringify({ root: work }),
} as never);
const created = (await res.json()) as Envelope<{ id: string }>;
expect(created.code).toBe(0);
const body = await postFs<{ items: { path: string }[]; truncated: boolean }>(
created.data.id,
'search',
{ query: 'gamma' },
);
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain('gamma.ts');
});
it('fs:search resolves an unregistered workspace root path', async () => {
await writeFile(join(work!, 'delta.ts'), '');
const body = await postFs<{ items: { path: string }[]; truncated: boolean }>(
encodeURIComponent(work!),
'search',
{ query: 'delta' },
);
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain('delta.ts');
});
it('fs:search still maps an unknown ref to SESSION_NOT_FOUND', async () => {
const body = await postFs<null>('does-not-exist', 'search', { query: 'x' });
expect(body.code).toBe(ErrorCode.SESSION_NOT_FOUND);
});
it('fs:grep finds matching lines', async () => {
await writeFile(join(work!, 'a.txt'), 'hello world\nfoo bar\n');
const id = await createSession();
const body = await postFs<{ files: { path: string; matches: unknown[] }[] }>(
id,
'grep',
{ pattern: 'hello' },
);
expect(body.code).toBe(0);
expect(body.data.files.length).toBeGreaterThanOrEqual(1);
});
it('fs:git_status maps a non-git workspace to FS_GIT_UNAVAILABLE', async () => {
const id = await createSession();
const body = await postFs<null>(id, 'git_status', {});
expect(body.code).toBe(ErrorCode.FS_GIT_UNAVAILABLE);
});
it('rejects an unknown action with VALIDATION_FAILED', async () => {
const id = await createSession();
const body = await postFs<null>(id, 'bogus', {});
expect(body.code).toBe(ErrorCode.VALIDATION_FAILED);
});
it('maps an unknown session to SESSION_NOT_FOUND', async () => {
const body = await postFs<null>('does-not-exist', 'stat', { path: 'a.txt' });
expect(body.code).toBe(ErrorCode.SESSION_NOT_FOUND);
});
it('rejects a path that escapes the workspace', async () => {
const id = await createSession();
const body = await postFs<null>(id, 'stat', { path: '../etc/passwd' });
expect(body.code).toBe(ErrorCode.FS_PATH_ESCAPES_SESSION);
});
it('rejects reads and downloads that escape the workspace through a symlink', async () => {
const outside = await mkdtemp(join(tmpdir(), 'kimi-server-v2-fs-outside-'));
try {
await writeFile(join(outside, 'secret.txt'), 'top-secret');
await symlink(outside, join(work!, 'docs'), 'dir');
const id = await createSession();
const body = await postFs<null>(id, 'read', { path: 'docs/secret.txt' });
expect(body.code).toBe(ErrorCode.FS_PATH_ESCAPES_SESSION);
const res = await fetch(`${base}/api/v1/sessions/${id}/fs/docs/secret.txt:download?runtime_id=local`, {
headers: authHeaders(server as RunningServer),
} as never);
const downloadBody = (await res.json()) as Envelope<null>;
expect(downloadBody.code).toBe(ErrorCode.FS_PATH_ESCAPES_SESSION);
} finally {
await rm(outside, { recursive: true, force: true });
}
});
it('serves fs actions when the session cwd itself goes through a symlink', async () => {
const link = join(tmpdir(), `kimi-server-v2-fs-cwd-link-${process.pid}`);
await symlink(work!, link, 'dir');
try {
const res = await fetch(`${base}/api/v1/sessions`, {
method: 'POST',
headers: authHeaders(server as RunningServer, { 'content-type': 'application/json' }),
body: JSON.stringify({ metadata: { cwd: link } }),
} as never);
const body = (await res.json()) as Envelope<{ id: string }>;
expect(body.code).toBe(0);
await writeFile(join(work!, 'via-link.txt'), 'through-link');
const read = await postFs<{ content: string }>(body.data.id, 'read', {
path: 'via-link.txt',
});
expect(read.code).toBe(0);
expect(read.data.content).toBe('through-link');
} finally {
await rm(link, { force: true });
}
});
async function postWorkspaceSearch<T>(body: unknown): Promise<Envelope<T>> {
const res = await fetch(`${base}/api/v1/workspace/fs:search`, {
method: 'POST',
headers: authHeaders(server as RunningServer, { 'content-type': 'application/json' }),
body: JSON.stringify({ runtime_id: 'local', ...(body as object) }),
} as never);
return (await res.json()) as Envelope<T>;
}
it('workspace fs:search finds files by registered workspace id', async () => {
await writeFile(join(work!, 'epsilon.ts'), '');
const res = await fetch(`${base}/api/v1/workspaces`, {
method: 'POST',
headers: authHeaders(server as RunningServer, { 'content-type': 'application/json' }),
body: JSON.stringify({ root: work }),
} as never);
const created = (await res.json()) as Envelope<{ id: string }>;
expect(created.code).toBe(0);
const body = await postWorkspaceSearch<{ items: { path: string }[]; truncated: boolean }>({
workspace: created.data.id,
query: 'epsilon',
});
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain('epsilon.ts');
});
it('workspace fs:search finds files by absolute root path', async () => {
await writeFile(join(work!, 'zeta.ts'), '');
const body = await postWorkspaceSearch<{ items: { path: string }[]; truncated: boolean }>({
workspace: work,
query: 'zeta',
});
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain('zeta.ts');
});
it('workspace fs:search lists top-level entries for an empty query', async () => {
await writeFile(join(work!, 'eta.ts'), '');
const body = await postWorkspaceSearch<{ items: { path: string }[]; truncated: boolean }>({
workspace: work,
query: '',
});
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain('eta.ts');
});
it('workspace fs:search defaults to the local runtime when runtime_id is omitted', async () => {
await writeFile(join(work!, 'theta.ts'), '');
const res = await fetch(`${base}/api/v1/workspace/fs:search`, {
method: 'POST',
headers: authHeaders(server as RunningServer, { 'content-type': 'application/json' }),
body: JSON.stringify({ workspace: work, query: 'theta' }),
} as never);
const body = (await res.json()) as Envelope<{ items: { path: string }[]; truncated: boolean }>;
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain('theta.ts');
});
it('workspace fs:search maps an unknown ref to WORKSPACE_NOT_FOUND', async () => {
const body = await postWorkspaceSearch<null>({ workspace: 'does-not-exist', query: 'x' });
expect(body.code).toBe(ErrorCode.WORKSPACE_NOT_FOUND);
});
it('workspace fs:search rejects a missing workspace field with VALIDATION_FAILED', async () => {
const body = await postWorkspaceSearch<null>({ query: 'x' });
expect(body.code).toBe(ErrorCode.VALIDATION_FAILED);
});
interface SuggestItemWire {
path: string;
name: string;
kind: string;
score: number;
match_positions: number[];
}
async function postWorkspaceSuggest<T>(body: unknown): Promise<Envelope<T>> {
const res = await fetch(`${base}/api/v1/workspace/fs:suggest`, {
method: 'POST',
headers: authHeaders(server as RunningServer, { 'content-type': 'application/json' }),
body: JSON.stringify({ runtime_id: 'local', ...(body as object) }),
} as never);
return (await res.json()) as Envelope<T>;
}
it('workspace fs:suggest finds files by registered workspace id', async () => {
await writeFile(join(work!, 'epsilon.ts'), '');
const res = await fetch(`${base}/api/v1/workspaces`, {
method: 'POST',
headers: authHeaders(server as RunningServer, { 'content-type': 'application/json' }),
body: JSON.stringify({ root: work }),
} as never);
const created = (await res.json()) as Envelope<{ id: string }>;
expect(created.code).toBe(0);
const body = await postWorkspaceSuggest<{ items: SuggestItemWire[]; truncated: boolean }>({
workspace: created.data.id,
query: 'epsilon',
});
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain('epsilon.ts');
});
it('workspace fs:suggest finds files by absolute root path', async () => {
await writeFile(join(work!, 'zeta.ts'), '');
const body = await postWorkspaceSuggest<{ items: SuggestItemWire[]; truncated: boolean }>({
workspace: work,
query: 'zeta',
});
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain('zeta.ts');
});
it('workspace fs:suggest lists top-level entries for an empty query', async () => {
await writeFile(join(work!, 'eta.ts'), '');
const body = await postWorkspaceSuggest<{ items: SuggestItemWire[]; truncated: boolean }>({
workspace: work,
query: '',
});
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain('eta.ts');
});
it('workspace fs:suggest matches path segments and returns scored items', async () => {
await mkdir(join(work!, 'apps'));
await mkdir(join(work!, 'apps', 'desktop'));
await writeFile(join(work!, 'apps', 'desktop', 'package.json'), '{}');
const body = await postWorkspaceSuggest<{ items: SuggestItemWire[]; truncated: boolean }>({
workspace: work,
query: 'apps/de',
});
expect(body.code).toBe(0);
expect(body.data.items.length).toBeGreaterThan(0);
expect(body.data.items[0]?.path).toBe('apps/desktop');
expect(body.data.items[0]?.kind).toBe('directory');
expect(body.data.items.map((i) => i.path)).toContain('apps/desktop/package.json');
for (const item of body.data.items) {
expect(item.score).toBeGreaterThan(0);
expect(item.score).toBeLessThanOrEqual(1);
expect(Array.isArray(item.match_positions)).toBe(true);
}
});
it('workspace fs:suggest returns an empty list when a path-form query has no match', async () => {
const body = await postWorkspaceSuggest<{ items: SuggestItemWire[]; truncated: boolean }>({
workspace: work,
query: 'zzz/qqq',
});
expect(body.code).toBe(0);
expect(body.data.items).toEqual([]);
expect(body.data.truncated).toBe(false);
});
it('workspace fs:suggest hides dotfiles by default and shows them with show_hidden', async () => {
await writeFile(join(work!, '.theta.ts'), '');
const hidden = await postWorkspaceSuggest<{ items: SuggestItemWire[] }>({
workspace: work,
query: 'theta',
});
expect(hidden.code).toBe(0);
expect(hidden.data.items.map((i) => i.path)).not.toContain('.theta.ts');
const shown = await postWorkspaceSuggest<{ items: SuggestItemWire[] }>({
workspace: work,
query: 'theta',
show_hidden: true,
});
expect(shown.code).toBe(0);
expect(shown.data.items.map((i) => i.path)).toContain('.theta.ts');
});
it('workspace fs:suggest defaults to the local runtime when runtime_id is omitted', async () => {
await writeFile(join(work!, 'iota.ts'), '');
const res = await fetch(`${base}/api/v1/workspace/fs:suggest`, {
method: 'POST',
headers: authHeaders(server as RunningServer, { 'content-type': 'application/json' }),
body: JSON.stringify({ workspace: work, query: 'iota' }),
} as never);
const body = (await res.json()) as Envelope<{ items: SuggestItemWire[]; truncated: boolean }>;
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain('iota.ts');
});
it('workspace fs:suggest maps an unknown ref to WORKSPACE_NOT_FOUND', async () => {
const body = await postWorkspaceSuggest<null>({ workspace: 'does-not-exist', query: 'x' });
expect(body.code).toBe(ErrorCode.WORKSPACE_NOT_FOUND);
});
it('workspace fs:suggest rejects a missing workspace field with VALIDATION_FAILED', async () => {
const body = await postWorkspaceSuggest<null>({ query: 'x' });
expect(body.code).toBe(ErrorCode.VALIDATION_FAILED);
});
it('workspace fs:suggest rejects a missing query field with VALIDATION_FAILED', async () => {
const body = await postWorkspaceSuggest<null>({ workspace: work });
expect(body.code).toBe(ErrorCode.VALIDATION_FAILED);
});
async function postRootSuggest<T>(body: unknown): Promise<Envelope<T>> {
const res = await fetch(`${base}/api/v1/fs:suggest`, {
method: 'POST',
headers: authHeaders(server as RunningServer, { 'content-type': 'application/json' }),
body: JSON.stringify(body),
} as never);
return (await res.json()) as Envelope<T>;
}
async function listWorkspaces(): Promise<{ id: string; root: string }[]> {
const res = await fetch(`${base}/api/v1/workspaces`, {
headers: authHeaders(server as RunningServer),
} as never);
const body = (await res.json()) as Envelope<{ items: { id: string; root: string }[] }>;
expect(body.code).toBe(0);
return body.data.items;
}
it('fs:suggest serves an unregistered root without touching the workspace catalog', async () => {
await writeFile(join(work!, 'kappa.ts'), '');
const body = await postRootSuggest<{ items: SuggestItemWire[]; truncated: boolean }>({
roots: [work],
query: 'kappa',
});
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain('kappa.ts');
const workAliases = [work!, await realpath(work!)];
expect((await listWorkspaces()).some((w) => workAliases.includes(w.root))).toBe(false);
expect(
server!.core.accessor
.get(IWorkspaceInstanceManager)
.list()
.some((w) => workAliases.includes(w.root)),
).toBe(false);
const again = await postRootSuggest<{ items: SuggestItemWire[] }>({
roots: [work],
query: 'kappa',
});
expect(again.code).toBe(0);
expect((await listWorkspaces()).some((w) => workAliases.includes(w.root))).toBe(false);
});
it('fs:suggest matches the workspace route for the same single root', async () => {
await mkdir(join(work!, 'apps'));
await mkdir(join(work!, 'apps', 'desktop'));
await writeFile(join(work!, 'apps', 'desktop', 'package.json'), '{}');
await writeFile(join(work!, 'README.md'), '');
const viaWorkspace = await postWorkspaceSuggest<{ items: SuggestItemWire[]; truncated: boolean }>({
workspace: work,
query: 'apps/de',
});
const viaRoots = await postRootSuggest<{ items: SuggestItemWire[]; truncated: boolean }>({
roots: [work],
query: 'apps/de',
});
expect(viaWorkspace.code).toBe(0);
expect(viaRoots.code).toBe(0);
expect(viaRoots.data).toEqual(viaWorkspace.data);
});
it('fs:suggest merges candidates across roots with relative paths for the primary root only', async () => {
const extra = await mkdtemp(join(tmpdir(), 'kimi-server-v2-fs-extra-'));
try {
await writeFile(join(work!, 'shared-name.ts'), '');
await mkdir(join(extra, 'lib'));
await writeFile(join(extra, 'lib', 'util.ts'), '');
const body = await postRootSuggest<{ items: SuggestItemWire[]; truncated: boolean }>({
roots: [work, extra],
query: 'util',
});
expect(body.code).toBe(0);
expect(body.data.items.map((i) => i.path)).toContain(join(extra, 'lib', 'util.ts').split(sep).join('/'));
const shared = await postRootSuggest<{ items: SuggestItemWire[] }>({
roots: [work, extra],
query: 'shared',
});
expect(shared.data.items.map((i) => i.path)).toContain('shared-name.ts');
} finally {
await rm(extra, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 });
}
});
it('fs:suggest lists top-level entries of every root for an empty query', async () => {
const extra = await mkdtemp(join(tmpdir(), 'kimi-server-v2-fs-extra-'));
try {
await writeFile(join(work!, 'top-work.ts'), '');
await writeFile(join(extra, 'top-extra.ts'), '');
const body = await postRootSuggest<{ items: SuggestItemWire[]; truncated: boolean }>({
roots: [work, extra],
query: '',
});
expect(body.code).toBe(0);
const paths = body.data.items.map((i) => i.path);
expect(paths).toContain('top-work.ts');
expect(paths).toContain(join(extra, 'top-extra.ts').split(sep).join('/'));
} finally {
await rm(extra, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 });
}
});
it('fs:suggest deduplicates overlapping roots', async () => {
await mkdir(join(work!, 'sub'));
await writeFile(join(work!, 'sub', 'dup.ts'), '');
await writeFile(join(work!, 'top.ts'), '');
const nested = await postRootSuggest<{ items: SuggestItemWire[] }>({
roots: [work, join(work!, 'sub')],
query: 'dup',
});
expect(nested.code).toBe(0);
expect(nested.data.items.filter((i) => i.path === join('sub', 'dup.ts').split(sep).join('/'))).toHaveLength(1);
const reversed = await postRootSuggest<{ items: SuggestItemWire[] }>({
roots: [join(work!, 'sub'), work],
query: 'dup',
});
expect(reversed.code).toBe(0);
expect(reversed.data.items.filter((i) => i.path === 'dup.ts')).toHaveLength(1);
const top = await postRootSuggest<{ items: SuggestItemWire[] }>({
roots: [join(work!, 'sub'), work],
query: 'top',
});
expect(top.data.items.map((i) => i.path)).toContain(join(work!, 'top.ts').split(sep).join('/'));
});
it('fs:suggest applies the limit to the merged ranking across roots', async () => {
const extra = await mkdtemp(join(tmpdir(), 'kimi-server-v2-fs-extra-'));
try {
await writeFile(join(work!, 'a1.ts'), '');
await writeFile(join(extra, 'a2.ts'), '');
await writeFile(join(extra, 'a3.ts'), '');
const body = await postRootSuggest<{ items: SuggestItemWire[]; truncated: boolean }>({
roots: [work, extra],
query: 'a',
limit: 2,
});
expect(body.code).toBe(0);
expect(body.data.items).toHaveLength(2);
expect(body.data.truncated).toBe(true);
} finally {
await rm(extra, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 });
}
});
it('fs:suggest honors follow_gitignore on each root', async () => {
const extra = await mkdtemp(join(tmpdir(), 'kimi-server-v2-fs-extra-'));
try {
await writeFile(join(extra, '.gitignore'), 'ignored-extra.ts\n');
await writeFile(join(extra, 'ignored-extra.ts'), '');
const followed = await postRootSuggest<{ items: SuggestItemWire[] }>({
roots: [extra],
query: 'ignored',
});
expect(followed.code).toBe(0);
expect(followed.data.items).toEqual([]);
const ignored = await postRootSuggest<{ items: SuggestItemWire[] }>({
roots: [extra],
query: 'ignored',
follow_gitignore: false,
});
expect(ignored.code).toBe(0);
expect(ignored.data.items.map((i) => i.path)).toContain('ignored-extra.ts');
} finally {
await rm(extra, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 });
}
});
it('fs:suggest maps a missing root to FS_PATH_NOT_FOUND', async () => {
const body = await postRootSuggest<null>({
roots: [join(work!, 'no-such-dir')],
query: 'x',
});
expect(body.code).toBe(ErrorCode.FS_PATH_NOT_FOUND);
});
it('fs:suggest maps a non-directory root to FS_PATH_NOT_FOUND', async () => {
await writeFile(join(work!, 'a-file.ts'), '');
const body = await postRootSuggest<null>({ roots: [join(work!, 'a-file.ts')], query: 'x' });
expect(body.code).toBe(ErrorCode.FS_PATH_NOT_FOUND);
});
it('fs:suggest rejects a relative root with VALIDATION_FAILED', async () => {
const body = await postRootSuggest<null>({ roots: ['relative/path'], query: 'x' });
expect(body.code).toBe(ErrorCode.VALIDATION_FAILED);
});
it('fs:suggest rejects a missing roots field with VALIDATION_FAILED', async () => {
const body = await postRootSuggest<null>({ query: 'x' });
expect(body.code).toBe(ErrorCode.VALIDATION_FAILED);
});
it('fs:suggest rejects more than 32 roots with VALIDATION_FAILED', async () => {
const roots = Array.from({ length: 33 }, (_, i) => join(work!, `root-${i}`));
const body = await postRootSuggest<null>({ roots, query: 'x' });
expect(body.code).toBe(ErrorCode.VALIDATION_FAILED);
});
it('fs:suggest rejects a missing query field with VALIDATION_FAILED', async () => {
const body = await postRootSuggest<null>({ roots: [work] });
expect(body.code).toBe(ErrorCode.VALIDATION_FAILED);
});
it('fs:suggest maps an unknown runtime to RUNTIME_NOT_FOUND', async () => {
const body = await postRootSuggest<null>({
roots: [work],
query: 'x',
runtime_id: 'no-such-runtime',
});
expect(body.code).toBe(ErrorCode.RUNTIME_NOT_FOUND);
});
});
|