285 lines
8.9 KiB
Rust
285 lines
8.9 KiB
Rust
use super::*;
|
|
|
|
#[test]
|
|
fn bounded_queue_backpressure_does_not_commit_terminal() {
|
|
let (sink, mut stream) = GenerationSink::bounded_channel(1).unwrap();
|
|
sink.try_emit(GenerationEvent::TextDelta {
|
|
text: "synthetic".into(),
|
|
})
|
|
.unwrap();
|
|
let terminal = GenerationEvent::Terminal(GenerationTerminal::Cancelled);
|
|
assert_eq!(
|
|
sink.try_emit(terminal.clone()),
|
|
Err(ContractError::GenerationBackpressure)
|
|
);
|
|
assert!(matches!(
|
|
stream.recv().unwrap(),
|
|
Some(GenerationEvent::TextDelta { .. })
|
|
));
|
|
sink.try_emit(terminal).unwrap();
|
|
assert_eq!(
|
|
sink.try_emit(GenerationEvent::Usage(Usage::default())),
|
|
Err(ContractError::GenerationAlreadyTerminal)
|
|
);
|
|
assert!(matches!(
|
|
stream.recv().unwrap(),
|
|
Some(GenerationEvent::Terminal(_))
|
|
));
|
|
assert_eq!(stream.recv().unwrap(), None);
|
|
}
|
|
#[test]
|
|
fn bounded_consumer_drop_is_observable_without_a_send() {
|
|
assert!(GenerationSink::bounded_channel(0).is_err());
|
|
assert!(GenerationSink::bounded_channel(17).is_err());
|
|
assert!(GenerationSink::bounded_channel(usize::MAX).is_err());
|
|
let (sink, stream) = GenerationSink::bounded_channel(1).unwrap();
|
|
assert!(!sink.is_closed());
|
|
drop(stream);
|
|
assert!(sink.is_closed());
|
|
assert_eq!(
|
|
sink.try_emit(GenerationEvent::Terminal(GenerationTerminal::Cancelled)),
|
|
Err(ContractError::GenerationClosed)
|
|
);
|
|
}
|
|
|
|
fn model() -> ModelDescriptor {
|
|
ModelDescriptor {
|
|
family: "fixture_alpha".into(),
|
|
architecture: "fixture_arch_a".into(),
|
|
artifact_digest: format!("sha256:{}", "a".repeat(64)),
|
|
weight_format: "fixture:format_a".into(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn descriptor_requires_exact_sha256_digest() {
|
|
let mut descriptor = model();
|
|
assert!(descriptor.validate().is_ok());
|
|
descriptor.artifact_digest = "sha256:abc".into();
|
|
assert_eq!(
|
|
descriptor.validate(),
|
|
Err(ContractError::InvalidField("artifact_digest"))
|
|
);
|
|
}
|
|
|
|
fn capabilities() -> BackendCapabilities {
|
|
BackendCapabilities {
|
|
supported_models: vec![
|
|
SupportedModelTuple {
|
|
family: "fixture_alpha".into(),
|
|
architecture: "fixture_arch_a".into(),
|
|
weight_format: "fixture:format_a".into(),
|
|
},
|
|
SupportedModelTuple {
|
|
family: "fixture_beta".into(),
|
|
architecture: "fixture_arch_b".into(),
|
|
weight_format: "fixture:format_b".into(),
|
|
},
|
|
],
|
|
max_concurrent_generations: 2,
|
|
supports_cancellation: true,
|
|
supports_tool_call_streaming: true,
|
|
}
|
|
}
|
|
|
|
/// How many of the 8 family x architecture x format combinations the capabilities admit, each
|
|
/// checked against the two explicit tuples.
|
|
fn count_admitted_combinations(capabilities: &BackendCapabilities) -> usize {
|
|
let mut admitted = 0;
|
|
for family in ["fixture_alpha", "fixture_beta"] {
|
|
for architecture in ["fixture_arch_a", "fixture_arch_b"] {
|
|
for weight_format in ["fixture:format_a", "fixture:format_b"] {
|
|
let descriptor = ModelDescriptor {
|
|
family: family.into(),
|
|
architecture: architecture.into(),
|
|
weight_format: weight_format.into(),
|
|
..model()
|
|
};
|
|
let expected = matches!(
|
|
(family, architecture, weight_format),
|
|
("fixture_alpha", "fixture_arch_a", "fixture:format_a")
|
|
| ("fixture_beta", "fixture_arch_b", "fixture:format_b")
|
|
);
|
|
let actual = capabilities.supports(&descriptor);
|
|
assert_eq!(actual, expected, "{descriptor:?}");
|
|
admitted += usize::from(actual);
|
|
}
|
|
}
|
|
}
|
|
admitted
|
|
}
|
|
|
|
/// Descriptors that differ from `model()` in exactly one field the capabilities do not list.
|
|
fn unknown_descriptors() -> [ModelDescriptor; 3] {
|
|
[
|
|
ModelDescriptor {
|
|
family: "fixture_unknown".into(),
|
|
..model()
|
|
},
|
|
ModelDescriptor {
|
|
architecture: "fixture_arch_unknown".into(),
|
|
..model()
|
|
},
|
|
ModelDescriptor {
|
|
weight_format: "fixture:unknown".into(),
|
|
..model()
|
|
},
|
|
]
|
|
}
|
|
|
|
#[test]
|
|
fn capabilities_admit_only_explicit_tuples_not_their_cartesian_product() {
|
|
let capabilities = capabilities();
|
|
assert_eq!(count_admitted_combinations(&capabilities), 2);
|
|
for unknown in unknown_descriptors() {
|
|
assert!(!capabilities.supports(&unknown));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn empty_capabilities_advertise_no_models() {
|
|
assert!(!BackendCapabilities::default().supports(&model()));
|
|
let empty = BackendCapabilities {
|
|
supported_models: Vec::new(),
|
|
..capabilities()
|
|
};
|
|
assert!(!empty.supports(&model()));
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_digests_are_refused_even_for_an_explicit_tuple() {
|
|
for digest in [
|
|
String::new(),
|
|
"sha256:abc".into(),
|
|
format!("sha256:{}", "a".repeat(63)),
|
|
format!("sha256:{}", "a".repeat(65)),
|
|
format!("sha256:{}", "z".repeat(64)),
|
|
format!("SHA256:{}", "a".repeat(64)),
|
|
format!("sha256:{} ", "a".repeat(64)),
|
|
] {
|
|
let descriptor = ModelDescriptor {
|
|
artifact_digest: digest,
|
|
..model()
|
|
};
|
|
assert_eq!(
|
|
descriptor.validate(),
|
|
Err(ContractError::InvalidField("artifact_digest"))
|
|
);
|
|
assert!(!capabilities().supports(&descriptor));
|
|
}
|
|
}
|
|
|
|
/// The descriptor and the capabilities (first supported tuple) with `invalid` as the value of `field`.
|
|
fn with_malformed_field(field: &str, invalid: &str) -> (ModelDescriptor, BackendCapabilities) {
|
|
let mut descriptor = model();
|
|
let mut capabilities = capabilities();
|
|
let supported = &mut capabilities.supported_models[0];
|
|
match field {
|
|
"family" => {
|
|
descriptor.family = invalid.into();
|
|
supported.family = invalid.into();
|
|
}
|
|
"architecture" => {
|
|
descriptor.architecture = invalid.into();
|
|
supported.architecture = invalid.into();
|
|
}
|
|
"weight_format" => {
|
|
descriptor.weight_format = invalid.into();
|
|
supported.weight_format = invalid.into();
|
|
}
|
|
_ => unreachable!(),
|
|
}
|
|
(descriptor, capabilities)
|
|
}
|
|
|
|
#[test]
|
|
fn malformed_fields_cannot_be_admitted_even_if_a_backend_lists_them() {
|
|
for invalid in [
|
|
"",
|
|
" ",
|
|
"fixture alpha",
|
|
" fixture",
|
|
"fixture ",
|
|
"fixture\n",
|
|
"fixture\0",
|
|
] {
|
|
for field in ["family", "architecture", "weight_format"] {
|
|
let (descriptor, capabilities) = with_malformed_field(field, invalid);
|
|
assert_eq!(
|
|
descriptor.validate(),
|
|
Err(ContractError::InvalidField(field))
|
|
);
|
|
assert!(!capabilities.supports(&descriptor), "{field}: {invalid:?}");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn supported_tuple_does_not_pin_or_verify_artifact_bytes() {
|
|
let descriptor = ModelDescriptor {
|
|
artifact_digest: format!("sha256:{}", "b".repeat(64)),
|
|
..model()
|
|
};
|
|
assert!(capabilities().supports(&descriptor));
|
|
}
|
|
|
|
#[test]
|
|
fn backend_api_version_is_explicit_and_exact() {
|
|
let current = BackendIdentity {
|
|
name: "test".into(),
|
|
implementation_version: "0.1.0".into(),
|
|
api_version: BACKEND_API_VERSION,
|
|
};
|
|
assert!(current.supports_current_api());
|
|
assert_eq!(BACKEND_API_VERSION, 2);
|
|
for api_version in [0, 1, 3, u16::MAX] {
|
|
let incompatible = BackendIdentity {
|
|
api_version,
|
|
..current.clone()
|
|
};
|
|
assert!(!incompatible.supports_current_api());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn stream_accepts_deltas_then_exactly_one_terminal() {
|
|
let (sink, mut stream) = GenerationSink::channel();
|
|
sink.emit(GenerationEvent::TextDelta { text: "hi".into() })
|
|
.unwrap();
|
|
sink.complete(FinishReason::Stop, Usage::default()).unwrap();
|
|
assert_eq!(sink.cancel(), Err(ContractError::GenerationAlreadyTerminal));
|
|
assert!(matches!(
|
|
stream.recv().unwrap(),
|
|
Some(GenerationEvent::TextDelta { .. })
|
|
));
|
|
assert!(matches!(
|
|
stream.recv().unwrap(),
|
|
Some(GenerationEvent::Terminal(_))
|
|
));
|
|
assert_eq!(stream.recv().unwrap(), None);
|
|
}
|
|
|
|
#[test]
|
|
fn dropped_producer_without_terminal_is_detected() {
|
|
let (sink, mut stream) = GenerationSink::channel();
|
|
sink.emit(GenerationEvent::Usage(Usage::default())).unwrap();
|
|
drop(sink);
|
|
assert!(matches!(
|
|
stream.recv().unwrap(),
|
|
Some(GenerationEvent::Usage(_))
|
|
));
|
|
assert_eq!(stream.recv(), Err(StreamError::MissingTerminal));
|
|
}
|
|
|
|
#[test]
|
|
fn deltas_after_terminal_are_rejected() {
|
|
let (sink, _stream) = GenerationSink::channel();
|
|
sink.cancel().unwrap();
|
|
assert_eq!(
|
|
sink.emit(GenerationEvent::TextDelta {
|
|
text: "late".into()
|
|
}),
|
|
Err(ContractError::GenerationAlreadyTerminal)
|
|
);
|
|
}
|