Files
waggle-os/packages/server/tests/local/embedding-routing.test.ts
shumeiko.it fe2eb0f648 refactor: remove the billing tier model (P0.1)
The community edition has no monetization (ADR-0001). Remove the tier
model (packages/shared/src/tiers.ts), the requireTier middleware and every
tier gate, GET/PATCH /api/tier and the trial start, and the Stripe
integration, which depends on the tier model.

Personal features a tier used to limit are open to everyone: unlimited
workspaces and connectors, every embedding provider including LiteLLM,
per-workspace costs, Approvals. The workspace session cap is one constant
(100). Team and enterprise surfaces are hidden instead of unlocked (team
dock zone, Settings Team/Enterprise/Plan tabs, workspace Team tab);
cloud sync reports unavailable, as it did for free users.

The sidecar ignores the tier keys in config.json but does not delete them:
both editions may still share ~/.waggle.

Web billing UI is deleted: plan cards, upgrade and trial modals, payment
success route, the KVARK sales block, tier 403 handling.
2026-10-15 19:31:50 +02:00

147 lines
5.9 KiB
TypeScript

/**
* Embedding routing API tests (steal #10) —
* GET /api/embedding/status (enriched: configuredProvider + envOverride)
* POST /api/embedding/provider (validate → persist → restartRequired)
*
* Note: vitest.setup.ts pins EMBEDDING_PROVIDER='mock' for the whole suite, so the
* env-override branch would otherwise 409 every write. We clear it per-test to
* exercise the real paths, and set it explicitly for the override test.
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { MindDB, SessionStore, FrameStore } from '@waggle/core';
import { buildLocalServer } from '../../src/local/index.js';
import type { FastifyInstance } from 'fastify';
import { injectWithAuth } from '../test-utils.js';
describe('Embedding routing API', () => {
let server: FastifyInstance;
let tmpDir: string;
const ORIGINAL_ENV = process.env.EMBEDDING_PROVIDER;
function readConfig(): Record<string, unknown> {
return JSON.parse(fs.readFileSync(path.join(tmpDir, 'config.json'), 'utf-8'));
}
beforeAll(async () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-embrouting-test-'));
fs.writeFileSync(path.join(tmpDir, 'config.json'), JSON.stringify({}));
fs.mkdirSync(path.join(tmpDir, 'skills'), { recursive: true });
fs.writeFileSync(path.join(tmpDir, 'skills', '.starter-installed'), 'test');
const personalPath = path.join(tmpDir, 'personal.mind');
const mind = new MindDB(personalPath);
const sessions = new SessionStore(mind);
const frames = new FrameStore(mind);
const s1 = sessions.create('embrouting-test');
frames.createIFrame(s1.gop_id, 'Embedding routing test frame', 'normal');
mind.close();
server = await buildLocalServer({ dataDir: tmpDir });
});
afterAll(async () => {
await server.close();
fs.rmSync(tmpDir, { recursive: true, force: true });
if (ORIGINAL_ENV === undefined) delete process.env.EMBEDDING_PROVIDER;
else process.env.EMBEDDING_PROVIDER = ORIGINAL_ENV;
});
// Default to the no-env-override path; the override test opts back in.
beforeEach(() => { delete process.env.EMBEDDING_PROVIDER; });
it('GET /api/embedding/status returns the enriched shape', async () => {
const res = await injectWithAuth(server, { method: 'GET', url: '/api/embedding/status' });
expect(res.statusCode).toBe(200);
const body = JSON.parse(res.body);
expect(typeof body.activeProvider).toBe('string');
expect(Array.isArray(body.availableProviders)).toBe(true);
expect(typeof body.dimensions).toBe('number');
expect(typeof body.modelName).toBe('string');
// Enrichment fields added by steal #10.
expect(body.configuredProvider).toBe('auto'); // fresh config → default
expect(typeof body.envOverride).toBe('boolean');
expect(body.envOverride).toBe(false); // beforeEach cleared the env var
});
it('POST /api/embedding/provider rejects an unknown provider with 400', async () => {
const res = await injectWithAuth(server, {
method: 'POST',
url: '/api/embedding/provider',
payload: { provider: 'banana' },
});
expect(res.statusCode).toBe(400);
});
it('POST /api/embedding/provider rejects "mock" with 400 (not user-selectable)', async () => {
const res = await injectWithAuth(server, {
method: 'POST',
url: '/api/embedding/provider',
payload: { provider: 'mock' },
});
expect(res.statusCode).toBe(400);
});
it('POST /api/embedding/provider accepts litellm (every provider is selectable)', async () => {
const res = await injectWithAuth(server, {
method: 'POST',
url: '/api/embedding/provider',
payload: { provider: 'litellm' },
});
expect(res.statusCode).toBe(200);
const body = JSON.parse(res.body);
expect(body.configuredProvider).toBe('litellm');
expect((readConfig().embedding as { provider?: string }).provider).toBe('litellm');
});
it('POST /api/embedding/provider persists "auto" (no restart required)', async () => {
const res = await injectWithAuth(server, {
method: 'POST',
url: '/api/embedding/provider',
payload: { provider: 'auto' },
});
expect(res.statusCode).toBe(200);
const body = JSON.parse(res.body);
expect(body.configuredProvider).toBe('auto');
expect(body.restartRequired).toBe(false);
expect((readConfig().embedding as { provider?: string }).provider).toBe('auto');
});
it('POST /api/embedding/provider persists an explicit provider with restartRequired', async () => {
const res = await injectWithAuth(server, {
method: 'POST',
url: '/api/embedding/provider',
payload: { provider: 'inprocess' },
});
expect(res.statusCode).toBe(200);
const body = JSON.parse(res.body);
expect(body.configuredProvider).toBe('inprocess');
// Active provider at boot is mock (test env), so switching needs a restart.
expect(body.restartRequired).toBe(true);
expect((readConfig().embedding as { provider?: string }).provider).toBe('inprocess');
});
it('POST /api/embedding/provider rejects the write when EMBEDDING_PROVIDER env is set', async () => {
process.env.EMBEDDING_PROVIDER = 'voyage';
// config currently persists 'inprocess' from the previous test.
const res = await injectWithAuth(server, {
method: 'POST',
url: '/api/embedding/provider',
payload: { provider: 'auto' },
});
expect(res.statusCode).toBe(409);
const body = JSON.parse(res.body);
expect(body.error).toBe('EMBEDDING_PROVIDER_ENV_OVERRIDE');
expect(body.envOverride).toBe(true);
// The env-forced write must not have changed the persisted choice.
expect((readConfig().embedding as { provider?: string }).provider).toBe('inprocess');
// And status reports the override too.
const statusRes = await injectWithAuth(server, { method: 'GET', url: '/api/embedding/status' });
expect(JSON.parse(statusRes.body).envOverride).toBe(true);
});
});