mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
0c8a36676a
## Why PR #13783 moved the `codex.rs` unit tests into `codex_tests.rs`. This applies the same extraction pattern across the rest of `codex-rs/core` so the production modules stay focused on runtime code instead of large inline test blocks. Keeping the tests in sibling files also makes follow-up edits easier to review because product changes no longer have to share a file with hundreds or thousands of lines of test scaffolding. ## What changed - replaced each inline `mod tests { ... }` in `codex-rs/core/src/**` with a path-based module declaration - moved each extracted unit test module into a sibling `*_tests.rs` file, using `mod_tests.rs` for `mod.rs` modules - preserved the existing `cfg(...)` guards and module-local structure so the refactor remains structural rather than behavioral ## Testing - `cargo test -p codex-core --lib` (`1653 passed; 0 failed; 5 ignored`) - `just fix -p codex-core` - `cargo fmt --check` - `cargo shear`
110 lines
3.3 KiB
Rust
110 lines
3.3 KiB
Rust
use super::*;
|
|
use pretty_assertions::assert_eq;
|
|
use serde::Serialize;
|
|
|
|
#[test]
|
|
fn id_token_info_parses_email_and_plan() {
|
|
#[derive(Serialize)]
|
|
struct Header {
|
|
alg: &'static str,
|
|
typ: &'static str,
|
|
}
|
|
let header = Header {
|
|
alg: "none",
|
|
typ: "JWT",
|
|
};
|
|
let payload = serde_json::json!({
|
|
"email": "user@example.com",
|
|
"https://api.openai.com/auth": {
|
|
"chatgpt_plan_type": "pro"
|
|
}
|
|
});
|
|
|
|
fn b64url_no_pad(bytes: &[u8]) -> String {
|
|
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes)
|
|
}
|
|
|
|
let header_b64 = b64url_no_pad(&serde_json::to_vec(&header).unwrap());
|
|
let payload_b64 = b64url_no_pad(&serde_json::to_vec(&payload).unwrap());
|
|
let signature_b64 = b64url_no_pad(b"sig");
|
|
let fake_jwt = format!("{header_b64}.{payload_b64}.{signature_b64}");
|
|
|
|
let info = parse_chatgpt_jwt_claims(&fake_jwt).expect("should parse");
|
|
assert_eq!(info.email.as_deref(), Some("user@example.com"));
|
|
assert_eq!(info.get_chatgpt_plan_type().as_deref(), Some("Pro"));
|
|
}
|
|
|
|
#[test]
|
|
fn id_token_info_parses_go_plan() {
|
|
#[derive(Serialize)]
|
|
struct Header {
|
|
alg: &'static str,
|
|
typ: &'static str,
|
|
}
|
|
let header = Header {
|
|
alg: "none",
|
|
typ: "JWT",
|
|
};
|
|
let payload = serde_json::json!({
|
|
"email": "user@example.com",
|
|
"https://api.openai.com/auth": {
|
|
"chatgpt_plan_type": "go"
|
|
}
|
|
});
|
|
|
|
fn b64url_no_pad(bytes: &[u8]) -> String {
|
|
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes)
|
|
}
|
|
|
|
let header_b64 = b64url_no_pad(&serde_json::to_vec(&header).unwrap());
|
|
let payload_b64 = b64url_no_pad(&serde_json::to_vec(&payload).unwrap());
|
|
let signature_b64 = b64url_no_pad(b"sig");
|
|
let fake_jwt = format!("{header_b64}.{payload_b64}.{signature_b64}");
|
|
|
|
let info = parse_chatgpt_jwt_claims(&fake_jwt).expect("should parse");
|
|
assert_eq!(info.email.as_deref(), Some("user@example.com"));
|
|
assert_eq!(info.get_chatgpt_plan_type().as_deref(), Some("Go"));
|
|
}
|
|
|
|
#[test]
|
|
fn id_token_info_handles_missing_fields() {
|
|
#[derive(Serialize)]
|
|
struct Header {
|
|
alg: &'static str,
|
|
typ: &'static str,
|
|
}
|
|
let header = Header {
|
|
alg: "none",
|
|
typ: "JWT",
|
|
};
|
|
let payload = serde_json::json!({ "sub": "123" });
|
|
|
|
fn b64url_no_pad(bytes: &[u8]) -> String {
|
|
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes)
|
|
}
|
|
|
|
let header_b64 = b64url_no_pad(&serde_json::to_vec(&header).unwrap());
|
|
let payload_b64 = b64url_no_pad(&serde_json::to_vec(&payload).unwrap());
|
|
let signature_b64 = b64url_no_pad(b"sig");
|
|
let fake_jwt = format!("{header_b64}.{payload_b64}.{signature_b64}");
|
|
|
|
let info = parse_chatgpt_jwt_claims(&fake_jwt).expect("should parse");
|
|
assert!(info.email.is_none());
|
|
assert!(info.get_chatgpt_plan_type().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn workspace_account_detection_matches_workspace_plans() {
|
|
let workspace = IdTokenInfo {
|
|
chatgpt_plan_type: Some(PlanType::Known(KnownPlan::Business)),
|
|
..IdTokenInfo::default()
|
|
};
|
|
assert_eq!(workspace.is_workspace_account(), true);
|
|
|
|
let personal = IdTokenInfo {
|
|
chatgpt_plan_type: Some(PlanType::Known(KnownPlan::Pro)),
|
|
..IdTokenInfo::default()
|
|
};
|
|
assert_eq!(personal.is_workspace_account(), false);
|
|
}
|