File size: 2,690 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Plugin test runner — tests all registered hooks with mock context.
 *
 * @module plugins/testRunner
 */

import { loadPlugin, type LoadedPlugin } from "./loader";
import type { PluginManifestWithDefaults } from "./manifest";
import type { PluginContext } from "./hooks";
import { logger } from "../../../open-sse/utils/logger.ts";

const log = logger("PLUGIN_TEST_RUNNER");

export interface PluginTestResult {
  hook: string;
  passed: boolean;
  durationMs: number;
  error?: string;
  output?: unknown;
}

const MOCK_CONTEXT: PluginContext = {
  requestId: "test-req-001",
  body: { model: "gpt-4", messages: [{ role: "user", content: "test" }] },
  model: "gpt-4",
  provider: "openai",
  metadata: { test: true },
};

/**
 * Test all registered hooks for a plugin.
 */
export async function testPlugin(
  entryPoint: string,
  manifest: PluginManifestWithDefaults
): Promise<PluginTestResult[]> {
  const results: PluginTestResult[] = [];
  let loaded: LoadedPlugin | null = null;

  try {
    loaded = await loadPlugin(entryPoint, manifest);

    const hooksToTest: Array<{ name: string; call: () => Promise<unknown> }> = [];

    if (loaded.plugin.onRequest) {
      hooksToTest.push({ name: "onRequest", call: async () => { await loaded!.plugin.onRequest!(MOCK_CONTEXT); } });
    }
    if (loaded.plugin.onResponse) {
      hooksToTest.push({
        name: "onResponse",
        call: () => loaded!.plugin.onResponse!(MOCK_CONTEXT, { choices: [{ message: { content: "test" } }] }),
      });
    }
    if (loaded.plugin.onError) {
      hooksToTest.push({
        name: "onError",
        call: () => loaded!.plugin.onError!(MOCK_CONTEXT, new Error("test error")),
      });
    }

    for (const hook of hooksToTest) {
      const start = performance.now();
      try {
        const output = await hook.call();
        const durationMs = Math.round(performance.now() - start);
        results.push({ hook: hook.name, passed: true, durationMs, output });
      } catch (err: unknown) {
        const durationMs = Math.round(performance.now() - start);
        results.push({
          hook: hook.name,
          passed: false,
          durationMs,
          error: err instanceof Error ? err.message : String(err),
        });
      }
    }
  } catch (err: unknown) {
    results.push({
      hook: "load",
      passed: false,
      durationMs: 0,
      error: err instanceof Error ? err.message : String(err),
    });
  } finally {
    if (loaded) loaded.cleanup();
  }

  log.info("testRunner.result", {
    pluginName: manifest.name,
    passed: results.filter((r) => r.passed).length,
    failed: results.filter((r) => !r.passed).length,
  });

  return results;
}