windowcapture
исходный код / Tools/TestTipBridge.cs

TestTipBridge.cs

69 строк · 3,266 байт · модуль Tools
 1// Headless round-trip test for the TSF TIP bridge (Helpers/TipBridge.cs) + the single-word brain
 2// (TextProcessor.CorrectWordForTip). Proves the localhost protocol the native TIP uses (POST
 3// /correctword over TCP 8766) end to end in C#, and that the bridge returns the same as a direct call.
 4// The C++ side (Tip/WCTip.cpp BrainCorrect) speaks the identical HTTP, so a green run here validates
 5// the protocol both halves share. Build (referencing the app assembly), then run from anywhere:
 6//   csc /out:bin\TestTipBridge.exe /r:bin\MediaCore.exe Tools\TestTipBridge.cs
 7//   bin\TestTipBridge.exe        (writes UTF-8 results to bin\_tiptest_out.txt)
 8using System;
 9using System.IO;
10using System.Net;
11using System.Text;
12using System.Threading;
13using WindowCapture.Helpers;
14
15class TestTipBridge
16{
17    static string Post(string word, string ctx)
18    {
19        var body = Encoding.UTF8.GetBytes(word + "\n" + ctx);
20        var req = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:" + TipBridge.Port + "/correctword");
21        req.Method = "POST"; req.Timeout = 5000; req.ContentType = "text/plain; charset=utf-8";
22        req.ContentLength = body.Length;
23        using (var s = req.GetRequestStream()) s.Write(body, 0, body.Length);
24        using (var r = (HttpWebResponse)req.GetResponse())
25        using (var sr = new StreamReader(r.GetResponseStream(), Encoding.UTF8))
26            return sr.ReadToEnd();
27    }
28
29    static void Main()
30    {
31        var outp = new StringBuilder();
32        // 1) touch TextProcessor -> static ctor -> Init() kicks off async dict loads; wait for readiness
33        string warm = TextProcessor.CorrectWordForTip("инициализация", "");
34        bool ready = false;
35        for (int i = 0; i < 40; i++) // up to ~20s
36        {
37            if (TextProcessor.CorrectWordForTip("превет", "") != "превет") { ready = true; break; }
38            Thread.Sleep(500);
39        }
40        outp.AppendLine("dict ready=" + ready);
41
42        // 2) start the bridge and round-trip several words; compare bridge vs direct call
43        TipBridge.Start();
44        outp.AppendLine("bridge running=" + TipBridge.IsRunning);
45        Thread.Sleep(200);
46
47        var cases = new[] {
48            new[]{"превет",""}, new[]{"сабака",""}, new[]{"малако",""},
49            new[]{"привет",""}, new[]{"стол",""},
50        };
51        int matches = 0;
52        foreach (var c in cases)
53        {
54            string direct = TextProcessor.CorrectWordForTip(c[0], c[1]);
55            string bridge;
56            try { bridge = Post(c[0], c[1]); } catch (Exception ex) { bridge = "ERR:" + ex.Message; }
57            bool same = (direct == bridge);
58            if (same) matches++;
59            outp.AppendLine(string.Format("'{0}' -> direct='{1}' bridge='{2}' {3}",
60                c[0], direct, bridge, same ? "MATCH" : "DIFF"));
61        }
62        outp.AppendLine(string.Format("{0}/{1} bridge==direct", matches, cases.Length));
63        TipBridge.Stop();
64
65        string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "_tiptest_out.txt");
66        File.WriteAllText(path, outp.ToString(), new UTF8Encoding(false));
67        Console.WriteLine("DONE ready=" + ready + " matches=" + matches + " -> " + path);
68    }
69}