forked from mtusi2026/mtusi2026lab7
92 lines
3.2 KiB
TypeScript
92 lines
3.2 KiB
TypeScript
import { test, expect } from "bun:test";
|
|
import { createSignalAssertions } from "./openobserve-signals";
|
|
const parent = 1794785761292.7983,
|
|
traceId = "a".repeat(32);
|
|
function fixture() {
|
|
const calls: { kind: string; path: string; body: any }[] = [];
|
|
const at = BigInt(Math.trunc(parent) - 60000) * 1000000n;
|
|
const row = (
|
|
span: string,
|
|
previous: string,
|
|
operation: string,
|
|
service: string,
|
|
) => ({
|
|
trace_id: traceId,
|
|
span_id: span,
|
|
reference_parent_span_id: previous,
|
|
operation_name: operation,
|
|
service_name: service,
|
|
start_ns: at.toString(),
|
|
end_ns: (at + 10000n).toString(),
|
|
duration: 10,
|
|
_timestamp: Number(at / 1000n),
|
|
});
|
|
const request = async (kind: string, path: string, body?: unknown) => {
|
|
calls.push({ kind, path, body });
|
|
let data: unknown;
|
|
if (kind === "trace_streams") data = { list: [{ name: "messenger" }] };
|
|
else if (kind === "worker_trace") data = { hits: [{ trace_id: traceId }] };
|
|
else if (kind === "trace_chain")
|
|
data = {
|
|
hits: [
|
|
row("1", "", "POST /api/v1/attachments", "messenger-api"),
|
|
row("2", "1", "attachment.publish", "messenger-api"),
|
|
row("3", "2", "attachment.process", "messenger-worker"),
|
|
],
|
|
};
|
|
else if (kind === "correlated_logs")
|
|
data = {
|
|
hits: [
|
|
{ service_name: "messenger-api", request_id: "a".repeat(36) },
|
|
{ service_name: "messenger-worker" },
|
|
],
|
|
};
|
|
else data = { status: "success", data: { result: [{ value: [1, "1"] }] } };
|
|
return { status: 200, data };
|
|
};
|
|
return { calls, request };
|
|
}
|
|
test("reader produces fixed historical SQL end and the same PromQL time after three hours", async () => {
|
|
const { calls, request } = fixture();
|
|
const assert = createSignalAssertions({
|
|
now: () => parent + 3 * 3600000,
|
|
request,
|
|
});
|
|
const end = parent - 5000;
|
|
await assert(parent - 15 * 60000, end);
|
|
const searches = calls.filter((call) =>
|
|
call.path.startsWith("/api/default/_search"),
|
|
);
|
|
expect(searches.length).toBe(3);
|
|
for (const call of searches)
|
|
expect(call.body.query).toMatchObject({
|
|
start_time: Math.trunc((parent - 15 * 60000) * 1000),
|
|
end_time: Math.trunc(end * 1000),
|
|
});
|
|
const metrics = calls.filter((call) => call.kind.startsWith("metric_"));
|
|
expect(metrics.length).toBe(5);
|
|
for (const call of metrics)
|
|
expect(new URL("http://fixture" + call.path).searchParams.get("time")).toBe(
|
|
String(end / 1000),
|
|
);
|
|
});
|
|
test("fresh reader default uses the explicit clock for both native request forms", async () => {
|
|
const { calls, request } = fixture(),
|
|
now = Math.trunc(parent) + 1000;
|
|
await createSignalAssertions({ now: () => now, request })(now - 60000);
|
|
expect(
|
|
calls.find((call) => call.kind === "worker_trace")!.body.query.end_time,
|
|
).toBe(now * 1000);
|
|
expect(
|
|
new URL(
|
|
"http://fixture" +
|
|
calls.find((call) => call.kind === "metric_http_requests")!.path,
|
|
).searchParams.get("time"),
|
|
).toBe(String(now / 1000));
|
|
});
|
|
test("reader requires explicit native request and clock ports", () => {
|
|
expect(() =>
|
|
createSignalAssertions({ request: fixture().request } as any),
|
|
).toThrow("OPENOBSERVE_SIGNAL_PORTS_REQUIRED");
|
|
});
|