pub const GATEWAY_LABEL_ENV: &str = "TAPES_GATEWAY_LABEL";
pub const GATEWAY_LABEL_SUFFIX_ENV: &str = "TAPES_GATEWAY_LABEL_SUFFIX";
pub const GATEWAY_REMEDY_ENV: &str = "TAPES_GATEWAY_REMEDY";
pub const DEFAULT_LABEL: &str = "tapes";
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use super::super::{
GATEWAY_NONCE_ENV, GATEWAY_NONCE_HEADER, GATEWAY_PROVIDER_ROUTE_PREFIX,
GATEWAY_PROVIDER_ROUTES_ENV, GATEWAY_SCHEMA_ENV, GATEWAY_URL_ENV, PI_GATEWAY_EXTENSION,
provider_route, split_provider_route,
};
use super::*;
fn asset() -> &'static str {
PI_GATEWAY_EXTENSION.contents()
}
fn declared_const(name: &str) -> String {
let prefix = format!("const {name} = \"");
let at = asset()
.find(&prefix)
.unwrap_or_else(|| panic!("the asset declares no {name}"));
let rest = &asset()[at + prefix.len()..];
let end = rest
.find("\";")
.unwrap_or_else(|| panic!("the asset's {name} declaration is unterminated"));
rest[..end].to_string()
}
fn code_lines() -> Vec<String> {
asset()
.lines()
.map(|line| match line.find("//") {
Some(at) => line[..at].to_string(),
None => line.to_string(),
})
.filter(|line| !line.trim().is_empty())
.collect()
}
#[test]
fn the_asset_is_a_finished_file_and_not_a_template() {
assert!(
!asset().contains("__TAPES_"),
"the asset carries a slot placeholder; it is a template again, and \
a template means one rendering per product means two installed files"
);
}
#[test]
fn the_asset_reads_the_shared_gateway_environment_contract() {
assert_eq!(declared_const("GATEWAY_URL_ENV"), GATEWAY_URL_ENV);
assert_eq!(declared_const("GATEWAY_SCHEMA_ENV"), GATEWAY_SCHEMA_ENV);
assert_eq!(declared_const("GATEWAY_NONCE_ENV"), GATEWAY_NONCE_ENV);
assert_eq!(declared_const("GATEWAY_NONCE_HEADER"), GATEWAY_NONCE_HEADER);
}
#[test]
fn the_asset_reads_the_presentation_contract_at_runtime() {
assert_eq!(declared_const("GATEWAY_LABEL_ENV"), GATEWAY_LABEL_ENV);
assert_eq!(
declared_const("GATEWAY_LABEL_SUFFIX_ENV"),
GATEWAY_LABEL_SUFFIX_ENV
);
assert_eq!(declared_const("GATEWAY_REMEDY_ENV"), GATEWAY_REMEDY_ENV);
assert_eq!(declared_const("DEFAULT_LABEL"), DEFAULT_LABEL);
for identifier in [
"GATEWAY_LABEL_ENV",
"GATEWAY_LABEL_SUFFIX_ENV",
"GATEWAY_REMEDY_ENV",
] {
assert!(
asset().contains(&format!("process.env[{identifier}]")),
"the asset declares {identifier} but never reads it"
);
}
}
#[test]
fn the_asset_reads_the_provider_routing_contract() {
assert_eq!(
declared_const("GATEWAY_PROVIDER_ROUTES_ENV"),
GATEWAY_PROVIDER_ROUTES_ENV
);
assert_eq!(
declared_const("PROVIDER_ROUTE_PREFIX"),
GATEWAY_PROVIDER_ROUTE_PREFIX
);
assert!(
asset().contains(&format!("process.env[{}]", "GATEWAY_PROVIDER_ROUTES_ENV")),
"the asset declares the routing variable but never reads it"
);
}
#[test]
fn a_launcher_that_asks_for_nothing_gets_unlabelled_registrations() {
let opening = "providerRoutes ? `";
let at = asset()
.find(opening)
.expect("the asset does not gate the provider label on the routing variable");
let rest = &asset()[at + opening.len()..];
let labelled = &rest[..rest.find('`').expect("unterminated provider base URL")];
assert_eq!(labelled, "${baseUrl}${PROVIDER_ROUTE_PREFIX}/${provider}");
let otherwise = rest[rest.find('`').unwrap() + 1..]
.trim_start()
.strip_prefix(':')
.expect("the routing conditional has no unlabelled arm")
.trim_start();
assert!(
otherwise.starts_with("baseUrl"),
"the unlabelled arm is not the bare base URL: {otherwise:?}"
);
}
#[test]
fn the_schema_mismatch_warning_stands_down_under_provider_routes() {
assert!(
asset().contains("if (!providerRoutes && schemaProvider &&"),
"the schema-mismatch warning still fires when the proxy routes \
every provider it registers"
);
}
#[test]
fn the_route_the_asset_builds_is_the_route_the_contract_parses() {
for provider in ["anthropic", "openai", "openai-codex"] {
let base = provider_route(provider);
assert!(
base.starts_with(GATEWAY_PROVIDER_ROUTE_PREFIX),
"{base} is not under the declared prefix"
);
let requested = format!("{base}/v1/messages");
let (labelled, rest) = split_provider_route(&requested)
.expect("a route this contract built is not one it parses");
assert_eq!(labelled, provider);
assert_eq!(rest, "/v1/messages");
}
}
#[test]
fn presentation_values_reach_only_the_status_entry_and_the_notification() {
let sensitive = [
"registerProvider",
"GATEWAY_NONCE_HEADER",
"nonce",
"baseUrl",
"X-Tapes-",
"envelope",
"headers",
];
for identifier in ["statusLabel", "statusSuffix", "schemaRemedy"] {
let lines: Vec<String> = code_lines()
.into_iter()
.filter(|line| line.contains(identifier))
.collect();
assert!(
lines.len() >= 2,
"{identifier} is declared but never used; this test would pass vacuously"
);
for (line, token) in lines
.iter()
.flat_map(|line| sensitive.iter().copied().map(move |token| (line, token)))
{
assert!(
!line.contains(token),
"{identifier} reaches {token:?} on {line:?}; a display string \
must not touch the capture path"
);
}
}
}
#[test]
fn the_status_label_is_composed_exactly_as_it_was_when_it_was_rendered() {
let opening = "ctx.ui.setStatus(statusLabel, `";
let at = asset()
.find(opening)
.expect("the asset does not set a status entry from the runtime label");
let rest = &asset()[at + opening.len()..];
let pattern = &rest[..rest.find("`);").expect("unterminated status label")];
assert_eq!(pattern, "${statusLabel}:${activeSchema}${statusSuffix}");
let label = pattern
.replace("${statusLabel}", "acme")
.replace("${activeSchema}", "anthropic")
.replace("${statusSuffix}", "+codex");
assert_eq!(
label, "acme:anthropic+codex",
"the label a consumer's launch presents has changed"
);
}
#[test]
fn the_fallbacks_are_neutral_and_name_the_variable_a_user_would_set() {
assert_eq!(declared_const("DEFAULT_LABEL"), DEFAULT_LABEL);
let remedy = asset()
.split_once("const DEFAULT_REMEDY =")
.expect("the asset declares no DEFAULT_REMEDY")
.1;
let remedy = &remedy[..remedy.find(';').expect("unterminated DEFAULT_REMEDY")];
assert!(
remedy.contains(GATEWAY_URL_ENV),
"the neutral remedy does not name {GATEWAY_URL_ENV}, so it tells a \
user nothing they can act on"
);
}
#[test]
fn the_asset_reads_deletes_and_echoes_the_nonce_in_that_order() {
assert!(
asset().contains("const nonce = process.env[GATEWAY_NONCE_ENV];"),
"the asset does not read the nonce from the environment"
);
assert!(
asset().contains("delete process.env[GATEWAY_NONCE_ENV];"),
"the asset does not delete the nonce from its environment; \
shell-tool subprocesses would inherit the secret"
);
assert!(
asset().contains("[GATEWAY_NONCE_HEADER]: nonce"),
"the asset does not echo the nonce under the header name"
);
let read = asset()
.find("process.env[GATEWAY_NONCE_ENV]")
.unwrap_or(usize::MAX);
let delete = asset()
.find("delete process.env[GATEWAY_NONCE_ENV]")
.unwrap_or(0);
assert!(
read < delete,
"the asset deletes the nonce before it reads it"
);
}
#[test]
fn the_asset_has_no_built_in_endpoint_to_fall_back_to() {
for literal in ["127.0.0.1", "localhost:", "DEFAULT_GATEWAY_URL"] {
assert!(
!asset().contains(literal),
"the asset carries {literal:?}; it must be inert without {GATEWAY_URL_ENV}"
);
}
assert!(
asset().contains("const rawBaseUrl = process.env[GATEWAY_URL_ENV];"),
"the asset must take its address from the launch and nowhere else"
);
}
}