File size: 1,209 Bytes
7c5ed63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { cp, mkdir, writeFile, rm } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';

/** Copy a fixture session into a temporary KIMI_CODE_HOME. */
export async function buildSessionFixture(name: string): Promise<{
  home: string;
  sessionDir: string;
  cleanup: () => Promise<void>;
}> {
  const src = new URL(`./sessions/${name}`, import.meta.url).pathname;
  const home = await mkdtemp();
  const sessionsDir = join(home, 'sessions', 'wd_test_000000000000');
  const sessionDir = join(sessionsDir, 'session_fixture');
  await mkdir(sessionsDir, { recursive: true });
  await cp(src, sessionDir, { recursive: true });

  // Write session_index.jsonl.
  await writeFile(
    join(home, 'session_index.jsonl'),
    JSON.stringify({
      sessionId: 'session_fixture',
      sessionDir,
      workDir: '/tmp/work',
    }) + '\n',
  );

  return {
    home,
    sessionDir,
    cleanup: () => rm(home, { recursive: true, force: true }),
  };
}

async function mkdtemp(): Promise<string> {
  const base = join(tmpdir(), `vis-fixture-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`);
  await mkdir(base, { recursive: true });
  return base;
}