File size: 6,695 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
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
/**
 * TDD: per-engine compression preview via engineId param.
 *
 * Auth pattern mirrors tests/unit/compression/compression-preview-auth.test.ts:
 * - Use makeManagementSessionRequest to create a JWT-auth'd Request.
 * - Set DATA_DIR to a temp dir and run the DB setup before importing the route.
 */
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

import { makeManagementSessionRequest } from "../../helpers/managementSession.ts";

// ─── temp DB isolation ────────────────────────────────────────────────────────

const TEST_DATA_DIR = fs.mkdtempSync(
  path.join(os.tmpdir(), "omniroute-compression-preview-engine-")
);
const originalDataDir = process.env.DATA_DIR;
const originalJwtSecret = process.env.JWT_SECRET;

process.env.DATA_DIR = TEST_DATA_DIR;

const core = await import("../../../src/lib/db/core.ts");
const settingsDb = await import("../../../src/lib/db/settings.ts");
const previewRoute = await import("../../../src/app/api/compression/preview/route.ts");

// ─── helpers ──────────────────────────────────────────────────────────────────

async function setupAuth(): Promise<void> {
  core.resetDbInstance();
  fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
  fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
  await settingsDb.updateSettings({
    requireLogin: true,
    setupComplete: true,
    password: "test-password-hash",
  });
}

/**
 * Build a message array whose content is a pretty-printed JSON string of a homogeneous
 * array of β‰₯8 rows. headroom's crushText() tries the whole string as a JSON array when
 * it starts with "[", so passing JSON.stringify(rows, null, 2) as content triggers
 * compaction. Pretty-printed JSON has whitespace so countTokens() also gives a
 * meaningful baseline to compare against the compact tabular output.
 */
function buildHeadroomMessages(): Array<{ role: string; content: string }> {
  const rows = Array.from({ length: 10 }, (_, i) => ({
    id: i + 1,
    name: `item-${i + 1}`,
    value: (i + 1) * 100,
    active: true,
  }));
  // Pretty-print so word-split countTokens() produces a non-trivial baseline count
  return [{ role: "user", content: JSON.stringify(rows, null, 2) }];
}

// ─── lifecycle ────────────────────────────────────────────────────────────────

test.beforeEach(async () => {
  process.env.DATA_DIR = TEST_DATA_DIR;
  await setupAuth();
});

test.after(() => {
  if (originalDataDir === undefined) delete process.env.DATA_DIR;
  else process.env.DATA_DIR = originalDataDir;
  if (originalJwtSecret === undefined) delete process.env.JWT_SECRET;
  else process.env.JWT_SECRET = originalJwtSecret;
  core.resetDbInstance();
  fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
});

// ─── tests ────────────────────────────────────────────────────────────────────

test("POST /api/compression/preview with engineId=headroom returns 200 with token savings", async () => {
  const messages = buildHeadroomMessages();

  const request = await makeManagementSessionRequest("http://localhost/api/compression/preview", {
    method: "POST",
    body: {
      engineId: "headroom",
      messages,
      mode: "stacked",
    },
  });

  const response = await previewRoute.POST(request);
  assert.equal(response.status, 200, `Expected 200, got ${response.status}`);

  const body = (await response.json()) as {
    originalTokens: number;
    compressedTokens: number;
    tokensSaved: number;
    savingsPct: number;
    techniquesUsed: string[];
    original: string;
    compressed: string;
    mode: string;
  };

  assert.ok(
    typeof body.originalTokens === "number" && body.originalTokens > 0,
    `originalTokens should be > 0, got: ${body.originalTokens}`
  );
  assert.ok(
    typeof body.compressedTokens === "number",
    `compressedTokens should be a number, got: ${body.compressedTokens}`
  );
  assert.ok(
    body.compressedTokens < body.originalTokens,
    `compressedTokens (${body.compressedTokens}) should be < originalTokens (${body.originalTokens}) β€” headroom should compact a 10-row homogeneous JSON array`
  );
  assert.ok(body.tokensSaved > 0, `tokensSaved should be > 0, got: ${body.tokensSaved}`);
  assert.ok(body.savingsPct > 0, `savingsPct should be > 0, got: ${body.savingsPct}`);

  // Response shape completeness checks
  assert.ok(typeof body.original === "string", "should have original field");
  assert.ok(typeof body.compressed === "string", "should have compressed field");
  assert.ok(Array.isArray(body.techniquesUsed), "techniquesUsed should be an array");
});

test("POST /api/compression/preview with engineId but no mode still works (mode defaults to stacked)", async () => {
  const messages = buildHeadroomMessages();

  const request = await makeManagementSessionRequest("http://localhost/api/compression/preview", {
    method: "POST",
    body: {
      engineId: "headroom",
      messages,
      // mode intentionally omitted β€” should default to "stacked" when engineId present
    },
  });

  const response = await previewRoute.POST(request);
  assert.equal(
    response.status,
    200,
    `Expected 200 when mode omitted with engineId, got ${response.status}`
  );

  const body = (await response.json()) as { tokensSaved: number };
  assert.ok(body.tokensSaved > 0, `tokensSaved should be > 0 even without explicit mode`);
});

test("POST /api/compression/preview without engineId still works (existing path untouched)", async () => {
  const request = await makeManagementSessionRequest("http://localhost/api/compression/preview", {
    method: "POST",
    body: {
      messages: [{ role: "user", content: "Hello world" }],
      mode: "standard",
    },
  });

  const response = await previewRoute.POST(request);
  assert.equal(
    response.status,
    200,
    `Existing path should still return 200, got ${response.status}`
  );

  const body = (await response.json()) as { original: string; mode: string };
  assert.ok(typeof body.original === "string", "should have original field");
  assert.equal(body.mode, "standard", "mode field should reflect the requested mode");
});