Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

test(typescript-sdk): read the endpoint claim off the requests the client makes

Aashishc70f61aae7db
diff --git a/sdk/typescript/test/docs.test.mjs b/sdk/typescript/test/docs.test.mjs
index fdb34bdb..be574c5b 100644
--- a/sdk/typescript/test/docs.test.mjs
+++ b/sdk/typescript/test/docs.test.mjs
@@ -11,6 +11,11 @@
* client hands to `fetch` is captured, and the table is compared against the keys that were really put
* on the wire. Nothing here transcribes the client's option list -- if `body()` gains or drops a control
* the table has to move with it, in both directions.
+ *
+ * The same seam holds the page's statement about the paths it calls. It promised `/v1/systemone` was
+ * "the only endpoint the client calls" in the sentence that introduces `health`, while `health()` had
+ * been issuing `GET /health` the whole time, so an exclusivity claim is now read back off the requests
+ * the client actually makes rather than taken on faith from the prose.
*/
import assert from 'node:assert/strict';
import { existsSync, readFileSync } from 'node:fs';
@@ -30,6 +35,9 @@ const RESULT = {
usage: { input_tokens: 4, output_tokens: 0 },
};
+/** The liveness answer, which is the only `/health` payload every caller is guaranteed to get. */
+const HEALTH = { status: 'ok' };
+
/** The request body the client would send for `options`, minus `state` and `questions`. */
async function controls(options) {
let body;
@@ -44,10 +52,34 @@ async function controls(options) {
return Object.keys(rest).sort();
}
+/** The `[method, path]` pairs one call really put on the wire, and the error it threw, if any. */
+async function attempt(run) {
+ const seen = [];
+ const laya = new Laya({ fetch: async (url, init) => {
+ const path = new URL(url).pathname;
+ seen.push([init.method, path]);
+ const payload = path === '/health' ? HEALTH : RESULT;
+ return new Response(JSON.stringify(payload), { headers: { 'content-type': 'application/json' } });
+ } });
+ let error = null;
+ try {
+ await run(laya);
+ } catch (cause) {
+ error = cause;
+ }
+ return { seen, error };
+}
+
+/** The page as prose, so a claim is checked against the sentence that makes it rather than the file. */
+function sentences(page) {
+ return page.split(/(?<=[.!?])\s+/);
+}
+
/** A value for every option the table names, chosen to survive the client's own validation. */
const OPTION_VALUES = { model: 'english', task: 'typed', lang: 'de', langGuess: 'en',
maxLen: 512, headMaxLen: 24, minConfidence: 0.8 };
+
function controlTable(page) {
const lines = page.split('\n');
const start = lines.indexOf('| option | request field |');
@@ -90,6 +122,49 @@ test('the design page names every control the client puts on the wire, and only
'a bare predict() sends state and questions only');
});
+test('the client calls exactly the endpoints the page says it calls', async () => {
+ const page = readFileSync(PAGE, 'utf8');
+ const paths = new Map();
+ for (const [method, run] of [['predict', l => l.predict('the state', QUESTIONS, {})],
+ ['health', l => l.health()]]) {
+ const { seen, error } = await attempt(run);
+ assert.equal(error, null, `${method}() must reach the fixture, got ${error && error.name}: ${error && error.message}`);
+ assert.equal(seen.length, 1, `${method}() made ${JSON.stringify(seen)}, and the page describes one request per call`);
+ paths.set(method, seen[0]);
+ }
+ assert.deepEqual([...paths], [['predict', ['POST', '/v1/systemone']], ['health', ['GET', '/health']]],
+ "the client's network surface moved; the page and this test describe POST /v1/systemone and GET /health");
+
+ // Each call has to be stated by a sentence that names the method and the path it reaches. The page
+ // used to promise `/v1/systemone` was "the only endpoint the client calls" in the same sentence that
+ // introduced `health`, while `health()` had been issuing `GET /health` the whole time.
+ for (const [method, [, path]] of paths) {
+ const stated = sentences(page).find(s => s.includes('`' + method + '`') && s.includes('`' + path + '`'));
+ assert.ok(stated, `the client sends ${paths.get(method)[0]} ${path} from ${method}(), which no sentence of the page pairs with that method`);
+ }
+
+ // An exclusivity claim is decidable at the same seam: "the only endpoint the client calls" holds only
+ // if the client calls one path, and "the only <kind> endpoint" holds only if the sentence names the
+ // path of every method of that kind. `health` probes; only `predict` carries a decision.
+ const KINDS = { inference: ['predict'] };
+ for (const sentence of sentences(page)) {
+ const claim = /\bthe only\b[^.]{0,40}?\bendpoints?\b/i.exec(sentence);
+ if (!claim) continue;
+ const kind = (/\bthe only\s+([\w-]+)\s+endpoints?\b/i.exec(sentence) || [])[1];
+ const covered = kind === undefined ? [...paths.keys()] : KINDS[kind.toLowerCase()];
+ assert.ok(covered, `the page claims ${JSON.stringify(claim[0])} and this test cannot say which client `
+ + `methods "${kind}" covers; add it to KINDS`);
+ if (kind === undefined) {
+ assert.equal(covered.length, 1, `the page calls one endpoint the only one the client calls, but the `
+ + `client calls ${covered.length}: ${JSON.stringify([...paths.values()])}`);
+ }
+ for (const method of covered) {
+ assert.ok(sentence.includes('`' + paths.get(method)[1] + '`'),
+ `the page claims ${JSON.stringify(claim[0])} without naming ${paths.get(method)[1]}, which ${method}() calls`);
+ }
+ }
+});
+
test('no sentence on the page claims a control it documents is unsupported', async () => {
const page = readFileSync(PAGE, 'utf8');
const fields = [...controlTable(page).values()];