:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
//! Self-contained STL coverage: builds tiny bytecode programs that call a single native function
//! and asserts the result. These replace the (omitted) Node-oracle parity harness for the STL
//! surface that carries the most regression risk — the crypto/base64 natives backed by new
//! dependencies, JSON round-tripping that must preserve key order, and first-class tuples.
use hogvm::{sync_execute, ExecutionContext, Program};
use serde_json::{json, Value};
// Opcode numeric values (mirror common/hogvm/python/operation.py).
const OP_CALL_GLOBAL: i64 = 2;
const OP_EQ: i64 = 11;
const OP_NOT_EQ: i64 = 12;
const OP_GT: i64 = 13;
const OP_GT_EQ: i64 = 14;
const OP_LT: i64 = 15;
const OP_LT_EQ: i64 = 16;
const OP_STRING: i64 = 32;
const OP_INTEGER: i64 = 33;
const OP_RETURN: i64 = 38;
const OP_ARRAY: i64 = 43;
const OP_TUPLE: i64 = 44;
/// Wrap a value-producing fragment in a program header + `RETURN` and execute it.
fn run(fragment: Vec<Value>) -> Value {
let mut bc = vec![json!("_H"), json!(1)];
bc.extend(fragment);
bc.push(json!(OP_RETURN));
let program = Program::new(bc).expect("valid program");
let ctx = ExecutionContext::with_defaults(program).with_globals(json!({}));
sync_execute(&ctx, false).expect("execution succeeds")
}
fn str_lit(s: &str) -> Vec<Value> {
vec![json!(OP_STRING), json!(s)]
}
fn int_lit(n: i64) -> Vec<Value> {
vec![json!(OP_INTEGER), json!(n)]
}
/// `name(args…)`. The compiler pushes args left-to-right, so `args[0]` is the first listed.
fn call(name: &str, args: &[Vec<Value>]) -> Vec<Value> {
let mut f = Vec::new();
for a in args {
f.extend(a.clone());
}
f.extend([json!(OP_CALL_GLOBAL), json!(name), json!(args.len() as i64)]);
f
}
fn collection(op: i64, elems: &[Vec<Value>]) -> Vec<Value> {
let mut f = Vec::new();
for e in elems {
f.extend(e.clone());
}
f.extend([json!(op), json!(elems.len() as i64)]);
f
}
/// Single-string-arg native returning a string — the shape most STL functions take.
fn call_str(name: &str, arg: &str) -> Value {
run(call(name, &[str_lit(arg)]))
}
#[test]
fn sortable_semver_arrays_support_every_comparison_operation() {
// Tilde, caret, and wildcard ranges compile to a lower-bound GT_EQ and an upper-bound LT.
let cases = [
("equals", OP_EQ, "1.2.3", "1.2.3", true),
("equals", OP_EQ, "1.2.3", "1.2.4", false),
("not equals", OP_NOT_EQ, "1.2.3", "1.2.4", true),
("not equals", OP_NOT_EQ, "1.2.3", "1.2.3", false),
("greater than", OP_GT, "2.0.0", "1.9.9", true),
("greater than", OP_GT, "1.9.9", "2.0.0", false),
("greater than or equal", OP_GT_EQ, "2.0.0", "2.0.0", true),
("greater than or equal", OP_GT_EQ, "1.9.9", "2.0.0", false),
("less than", OP_LT, "2.9.0", "2.10.0", true),
("less than", OP_LT, "2.10.0", "2.10.0", false),
("less than or equal", OP_LT_EQ, "2.10.0", "2.10.0", true),
("less than or equal", OP_LT_EQ, "2.10.1", "2.10.0", false),
];
for (label, operation, left, right, expected) in cases {
let mut comparison = call("sortableSemver", &[str_lit(right)]);
comparison.extend(call("sortableSemver", &[str_lit(left)]));
comparison.push(json!(operation));
assert_eq!(run(comparison), json!(expected), "{left} {label} {right}");
}
}
#[test]
fn crypto_and_encoding_known_answers() {
// Known-answer vectors — these are RFC/spec fixed, so they pin both our wiring and the
// md-5 / sha2 / base64 dependencies to the correct output.
let cases: &[(&str, &str, &str)] = &[
("md5Hex", "hello", "5d41402abc4b2a76b9719d911017c592"),
(
"sha256Hex",
"hello",
"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
),
(
"sha1Hex",
"hello",
"aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d",
),
("sha1Hex", "abc", "a9993e364706816aba3e25717850c26c9cd0d89d"),
("sha1Hex", "", "da39a3ee5e6b4b0d3255bfef95601890afd80709"),
("base64Encode", "hello", "aGVsbG8="),
("base64Decode", "aGVsbG8=", "hello"),
];
for (name, input, expected) in cases {
assert_eq!(call_str(name, input), json!(expected), "{name}({input:?})");
}
}
#[test]
fn hogql_sha1_returns_lowercase_hex_or_null() {
for (input, expected) in [
("abc", "a9993e364706816aba3e25717850c26c9cd0d89d"),
("", "da39a3ee5e6b4b0d3255bfef95601890afd80709"),
("🦔", "b1495135295ac36752f6579e93ba02140604590f"),
] {
assert_eq!(call_str("sha1", input), json!(expected));
}
assert_eq!(run(call("sha1", &[vec![json!(31)]])), Value::Null);
}
#[test]
fn hmac_sha1_matches_rfc_2202_vectors() {
// RFC 2202 HMAC-SHA1 test vectors. A two-element chain is a plain HMAC of key and message, so
// these pin the sha1 dependency and the chain wiring to the standard answers.
let key_0b = "\u{0b}".repeat(20);
let key_0c = "\u{0c}".repeat(20);
let cases: [(&str, &str, &str); 3] = [
(
&key_0b,
"Hi There",
"b617318655057264e28bc0b6fb378c8ef146be00",
),
(
"Jefe",
"what do ya want for nothing?",
"effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
),
(
&key_0c,
"Test With Truncation",
"4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
),
];
for (key, message, expected) in cases {
let arr = collection(OP_ARRAY, &[str_lit(key), str_lit(message)]);
assert_eq!(
run(call("sha1HmacChainHex", &[arr])),
json!(expected),
"sha1HmacChainHex([key, {message:?}])"
);
}
}
#[test]
fn hmac_sha1_chain_rekeys_and_encodes() {
// Three or more elements re-key each step with the previous raw digest, and the optional
// second argument selects the encoding — both must match the Python and Node implementations.
let data = || {
collection(
OP_ARRAY,
&[
str_lit("1"),
str_lit("string"),
str_lit("more"),
str_lit("keys"),
],
)
};
let cases: &[(&str, &str)] = &[
("hex", "e559ff0c3fc9c9e13a5b5d78fcd722b4f7ec6a9a"),
("base64", "5Vn/DD/JyeE6W114/NcitPfsapo="),
("base64url", "5Vn_DD_JyeE6W114_NcitPfsapo"),
];
for (encoding, expected) in cases {
assert_eq!(
run(call("sha1HmacChain", &[data(), str_lit(encoding)])),
json!(expected),
"sha1HmacChain(data, {encoding:?})"
);
}
// No encoding argument defaults to hex, matching sha1HmacChainHex.
assert_eq!(
run(call("sha1HmacChain", &[data()])),
run(call("sha1HmacChainHex", &[data()]))
);
}
#[test]
fn hmac_sha256_base64_key_matches_svix() {
// Known answer from the Svix webhook verification docs. The key decodes to bytes that are not
// valid UTF-8, so this fails if the key is read as a string.
let key = || str_lit("MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw");
let payload = || str_lit("msg_p5jXN8AQM9LWM0D4loKWxJek.1614265330.{\"test\": 2432232314}");
assert_eq!(
run(call(
"sha256HmacBase64Key",
&[key(), payload(), str_lit("base64")]
)),
json!("g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=")
);
assert_eq!(
run(call(
"sha256HmacBase64Key",
&[str_lit("YWI"), str_lit("data")]
)),
run(call(
"sha256HmacBase64Key",
&[str_lit("YWI="), str_lit("data")]
))
);
}
#[test]
fn base64_round_trips() {
let encoded = run(call("base64Encode", &[str_lit("Hello, World! 🦔")]));
let decoded = run(call(
"base64Decode",
&[call("base64Encode", &[str_lit("Hello, World! 🦔")])],
));
assert!(matches!(encoded, Value::String(_)));
assert_eq!(decoded, json!("Hello, World! 🦔"));
}
#[test]
fn string_natives() {
let cases: &[(&str, &[&str], Value)] = &[
("upper", &["aBc"], json!("ABC")),
("lower", &["aBc"], json!("abc")),
("trim", &[" hi "], json!("hi")),
("trimLeft", &[" hi "], json!("hi ")),
("trimRight", &[" hi "], json!(" hi")),
("concat", &["foo", "bar"], json!("foobar")),
("replaceAll", &["aaa", "a", "b"], json!("bbb")),
("replaceOne", &["aaa", "a", "b"], json!("baa")),
("length", &["hello"], json!(5)),
];
for (name, args, expected) in cases {
let arg_lits: Vec<Vec<Value>> = args.iter().map(|s| str_lit(s)).collect();
assert_eq!(run(call(name, &arg_lits)), *expected, "{name}({args:?})");
}
}
#[test]
fn type_coercion_natives() {
assert_eq!(run(call("toString", &[int_lit(42)])), json!("42"));
assert_eq!(run(call("toInt", &[str_lit("42")])), json!(42));
assert_eq!(run(call("toFloat", &[str_lit("1.5")])), json!(1.5));
}
#[test]
fn array_natives() {
let arr = || collection(OP_ARRAY, &[int_lit(1), int_lit(2), int_lit(3)]);
assert_eq!(run(call("length", &[arr()])), json!(3));
assert_eq!(run(call("has", &[arr(), int_lit(2)])), json!(true));
assert_eq!(run(call("has", &[arr(), int_lit(9)])), json!(false));
// arrayPushBack is pure (does not mutate in place) and appends.
assert_eq!(
run(call("arrayPushBack", &[arr(), int_lit(4)])),
json!([1, 2, 3, 4])
);
// indexOf is 1-based; 0 means "not found" (Node-aligned, asserted in arrays.hog too).
assert_eq!(run(call("indexOf", &[arr(), int_lit(3)])), json!(3));
assert_eq!(run(call("indexOf", &[arr(), int_lit(9)])), json!(0));
}
#[test]
fn json_round_trip_preserves_key_order() {
// The crux of the order-preserving HogJson deserialization: serde_json::Value would sort keys
// (BTreeMap); the VM must keep document order so re-stringifying is stable.
let result = run(call(
"jsonStringify",
&[call("jsonParse", &[str_lit(r#"{"b":1,"a":2,"c":3}"#)])],
));
let Value::String(s) = result else {
panic!("jsonStringify should return a string, got {result:?}");
};
let pos = |needle: &str| {
s.find(needle)
.unwrap_or_else(|| panic!("{needle} missing from {s}"))
};
assert!(
pos("\"b\"") < pos("\"a\"") && pos("\"a\"") < pos("\"c\""),
"expected insertion order b, a, c — got {s}"
);
}
#[test]
fn tuples_are_distinct_from_arrays() {
// A tuple prints with parens and reports its own type, unlike an array.
let tup = || collection(OP_TUPLE, &[int_lit(1), int_lit(2), int_lit(3)]);
assert_eq!(run(call("typeof", &[tup()])), json!("tuple"));
assert_eq!(
run(call("typeof", &[collection(OP_ARRAY, &[int_lit(1)])])),
json!("array")
);
// Tuples still index and report length like arrays.
assert_eq!(run(call("length", &[tup()])), json!(3));
}