forked from Blendman974/faceclaw
Add stats collection and first-run on-glasses onboarding app
This commit is contained in:
1 parent
84baee9c76
commit
406cefe0c5
73 files changed
+3906
-98
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { test } from "node:test";
|
||||
import { Pool } from "pg";
|
||||
import { saveReport } from "../src/db.ts";
|
||||
import { parseReport } from "../src/report.ts";
|
||||
import { sampleReport } from "./sample.ts";
|
||||
|
||||
// Needs a scratch database: set STATS_TEST_DATABASE to its name (the other
|
||||
// PG* variables pick the server). The tables are created if missing, and
|
||||
// emptied before the test.
|
||||
const database = process.env.STATS_TEST_DATABASE;
|
||||
|
||||
void test("stores a report once, with its counters and snapshot", { skip: !database && "STATS_TEST_DATABASE not set" }, async () => {
|
||||
const pool = new Pool({ database });
|
||||
try {
|
||||
await pool.query(readFileSync(new URL("../schema.sql", import.meta.url), "utf8"));
|
||||
await pool.query("truncate reports cascade");
|
||||
|
||||
const report = parseReport(sampleReport());
|
||||
assert.equal(await saveReport(pool, report), "saved");
|
||||
assert.equal(await saveReport(pool, report), "duplicate");
|
||||
|
||||
const counters = await pool.query(
|
||||
"select day::text, name, value::int from report_counters order by day, name",
|
||||
);
|
||||
assert.deepEqual(counters.rows, [
|
||||
{ day: "2026-10-09", name: "g2.connect", value: 3 },
|
||||
{ day: "2026-10-09", name: "g2.disconnect", value: 2 },
|
||||
{ day: "2026-10-10", name: "g2.connect", value: 1 },
|
||||
]);
|
||||
const snapshot = await pool.query("select key, value from report_snapshot order by key");
|
||||
assert.deepEqual(snapshot.rows, [
|
||||
{ key: "apikey.anthropic", value: false },
|
||||
{ key: "device.g2.paired", value: true },
|
||||
{ key: "setting.ui-font", value: "Roboto-Light.ttf:16" },
|
||||
]);
|
||||
const daily = await pool.query("select total::int, installs::int from daily_counters where name = 'g2.connect' and day = '2026-10-09'");
|
||||
assert.deepEqual(daily.rows, [{ total: 3, installs: 1 }]);
|
||||
} finally {
|
||||
await pool.end();
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,61 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { parseReport, ReportError } from "../src/report.ts";
|
||||
import { sampleReport } from "./sample.ts";
|
||||
|
||||
void test("accepts a well-formed report and normalizes ids and timestamps", () => {
|
||||
const report = parseReport(
|
||||
sampleReport({ reportId: "0F8FAD5B-D9CB-469F-A165-70867728950E", createdAt: "2026-10-10T08:00:00-04:00" }),
|
||||
);
|
||||
assert.equal(report.reportId, "0f8fad5b-d9cb-469f-a165-70867728950e");
|
||||
assert.equal(report.createdAt, "2026-10-10T12:00:00.000Z");
|
||||
assert.deepEqual(report.counters["2026-10-09"], { "g2.connect": 3, "g2.disconnect": 2 });
|
||||
assert.equal(report.snapshot["setting.ui-font"], "Roboto-Light.ttf:16");
|
||||
});
|
||||
|
||||
void test("accepts camelCase snapshot keys but only lowercase counter names", () => {
|
||||
assert.equal(parseReport(sampleReport({ snapshot: { "setting.display.timeFormat": "12h" } })).snapshot["setting.display.timeFormat"], "12h");
|
||||
assert.throws(() => parseReport(sampleReport({ counters: { "2026-10-10": { "g2.Connect": 1 } } })), ReportError);
|
||||
});
|
||||
|
||||
void test("ignores unknown fields", () => {
|
||||
const report = parseReport(sampleReport({ extra: "x" }));
|
||||
assert.equal("extra" in report, false);
|
||||
});
|
||||
|
||||
void test("rejects malformed reports", () => {
|
||||
const bad: Record<string, unknown>[] = [
|
||||
{ schema: 2 },
|
||||
{ level: "everything" },
|
||||
{ reportId: "not-a-uuid" },
|
||||
{ app: { version: "0.8.3; drop table", platform: "android", build: "official" } },
|
||||
{ app: { version: "0.8.3", platform: "Android", build: "official" } },
|
||||
{ createdAt: "yesterday" },
|
||||
{ counters: { "2026-13-45": { a: 1 } } },
|
||||
{ counters: { "2026-10-10": { "Bad Name": 1 } } },
|
||||
{ counters: { "2026-10-10": { a: -1 } } },
|
||||
{ counters: { "2026-10-10": { a: 1.5 } } },
|
||||
{ counters: [] },
|
||||
{ snapshot: { a: { nested: true } } },
|
||||
{ snapshot: { "bad key": true } },
|
||||
{ snapshot: { a: "x".repeat(201) } },
|
||||
{ snapshot: { a: Number.NaN } },
|
||||
];
|
||||
for (const overrides of bad) {
|
||||
assert.throws(() => parseReport(sampleReport(overrides)), ReportError, JSON.stringify(overrides));
|
||||
}
|
||||
assert.throws(() => parseReport(null), ReportError);
|
||||
assert.throws(() => parseReport([]), ReportError);
|
||||
});
|
||||
|
||||
void test("caps how many counters and snapshot keys a report may carry", () => {
|
||||
const manyDays: Record<string, Record<string, number>> = {};
|
||||
for (let day = 0; day < 101; day++) {
|
||||
manyDays[new Date(Date.UTC(2026, 0, 1 + day)).toISOString().slice(0, 10)] = { a: 1 };
|
||||
}
|
||||
assert.throws(() => parseReport(sampleReport({ counters: manyDays })), /too many days/);
|
||||
|
||||
const manyKeys: Record<string, boolean> = {};
|
||||
for (let index = 0; index < 501; index++) manyKeys[`k${index}`] = true;
|
||||
assert.throws(() => parseReport(sampleReport({ snapshot: manyKeys })), /too many snapshot keys/);
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
/** A valid report body, with top-level fields replaced by `overrides`. */
|
||||
export function sampleReport(overrides: Record<string, unknown> = {}): Record<string, unknown> {
|
||||
return {
|
||||
schema: 1,
|
||||
reportId: "0f8fad5b-d9cb-469f-a165-70867728950e",
|
||||
installId: "7c9e6679-7425-40de-944b-e07fc1f90ae7",
|
||||
level: "full",
|
||||
createdAt: "2026-10-10T12:00:00.000Z",
|
||||
app: { version: "0.8.3", platform: "android", build: "official" },
|
||||
counters: { "2026-10-09": { "g2.connect": 3, "g2.disconnect": 2 }, "2026-10-10": { "g2.connect": 1 } },
|
||||
snapshot: { "device.g2.paired": true, "apikey.anthropic": false, "setting.ui-font": "Roboto-Light.ttf:16" },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user