All files / scripts test-toml-parsing.mjs

0% Statements 0/104
0% Branches 0/1
0% Functions 0/1
0% Lines 0/104

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                 
/**
 * Test script to verify TOML parsing with actual config files
 */

import TOML from 'smol-toml';

// Use inline TOML strings for testing
const MAIN_CONFIG_TOML = `
[server]
host = "127.0.0.1"
port = 4200
websocket_port = 4200
websocket_path = "/ws"

[agent.defaults]
workspace = "~/.openfang/workspace"
default_model = "gpt-4"

[llm]
default_provider = "openai"
default_model = "gpt-4"
`;

const CHINESE_PROVIDERS_TOML = `
[[llm.providers]]
name = "zhipu"
display_name = "Zhipu GLM"
api_key = "\${ZHIPU_API_KEY}"
base_url = "https://open.bigmodel.cn/api/paas/v4"

[[llm.providers.models]]
id = "glm-4-plus"
alias = "GLM-4-Plus"
context_window = 128000

[[llm.providers]]
name = "qwen"
display_name = "Qwen"
api_key = "\${QWEN_API_KEY}"
base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1"

[[llm.providers.models]]
id = "qwen-max"
alias = "Qwen-Max"
`;

console.log('=== Testing TOML Parsing ===\n');

// Test 1: Parse main config
try {
  console.log('\n--- Test 1: Main config.toml ---');
  const mainConfig = TOML.parse(MAIN_CONFIG_TOML);
  console.log('Parsed successfully!');
  console.log('Server config:', JSON.stringify(mainConfig.server, null, 2));
  console.log('Agent defaults:', JSON.stringify(mainConfig.agent?.defaults, null, 2));
  console.log('LLM config:', JSON.stringify(mainConfig.llm, null, 2));

  // Check required fields
  if (!mainConfig.server?.host) throw new Error('Missing server.host');
  if (!mainConfig.server?.port) throw new Error('Missing server.port');
  if (!mainConfig.agent?.defaults?.workspace) throw new Error('Missing agent.defaults.workspace');
  if (!mainConfig.agent?.defaults?.default_model) throw new Error('Missing agent.defaults.default_model');
  if (!mainConfig.llm?.default_provider) throw new Error('Missing llm.default_provider');
  if (!mainConfig.llm?.default_model) throw new Error('Missing llm.default_model');

  console.log('All required fields present!');
} catch (error) {
  console.error('Failed to parse main config:', error.message);
  process.exit(1);
}

// Test 2: Parse chinese-providers.toml
try {
  console.log('\n--- Test 2: chinese-providers.toml ---');
  const chineseProviders = TOML.parse(CHINESE_PROVIDERS_TOML);
  console.log('Parsed successfully!');
  console.log('Number of providers:', chineseProviders.llm?.providers?.length || 0);

  // List providers
  if (chineseProviders.llm?.providers) {
    console.log('\nProviders found:');
    chineseProviders.llm.providers.forEach((provider, index) => {
      console.log(`  ${index + 1}. ${provider.name} (${provider.display_name || 'N/A'})`);
      if (provider.models) {
        console.log(`     Models: ${provider.models.length}`);
      }
    });
  }

  // Check for environment variable references
  const envVarPattern = /\$\{([^}]+)\}/g;
  const envVars = CHINESE_PROVIDERS_TOML.match(envVarPattern);
  if (envVars) {
    console.log('\nEnvironment variables referenced:');
    const uniqueVars = [...new Set(envVars)];
    uniqueVars.forEach(v => console.log(`  - ${v}`));
  }

} catch (error) {
  console.error('Failed to parse chinese-providers:', error.message);
  process.exit(1);
}

console.log('\n=== All TOML parsing tests passed! ===\n');