diff --git a/README.md b/README.md index 74629ad29..df401aef1 100644 --- a/README.md +++ b/README.md @@ -340,7 +340,7 @@ The common ones: | Variable | Default | Purpose | |---|---|---| | `CODETRIAL_WEB_ADDR` | `127.0.0.1:3000` | Listen address | -| `CODETRIAL_DURATION_MIN` | `45` | Interview length preselected in the lobby (10–90); see [interview length](docs/interview-length.md) | +| `CODETRIAL_DURATION_MIN` | `45` | Interview length the lobby preselects and `/api/token` grants, unsnapped, to a request that names none (10–90; text that is no whole number refuses startup); see [interview length](docs/interview-length.md) | | `GEMINI_LIVE_MODEL` | `gemini-3.1-flash-live-preview` | Realtime interviewer model | | `GEMINI_REPORT_MODEL` | `gemini-3.1-flash-lite` | Report model | | `CODETRIAL_MAX_INTERIM_REVIEWS` | `6` | Quiet-pause report-model reviews per interview; `0` disables them and `72` is the maximum | diff --git a/docs/interview-length.md b/docs/interview-length.md index 327e69354..9e46598e5 100644 --- a/docs/interview-length.md +++ b/docs/interview-length.md @@ -5,9 +5,30 @@ both. One rule matters more than the rest: the length offered and the length enforced come from one function, so they cannot drift apart. The lobby offers 30, 45, and 60 minutes, and the candidate picks one before -starting. `CODETRIAL_DURATION_MIN` is only the length preselected for a -candidate who does not choose; the endpoint accepts 10 to 90 either way, so a -deployment that lowers the default has not lowered the ceiling. +starting. `CODETRIAL_DURATION_MIN` is the length preselected for a candidate who +does not choose, once `/api/session` has answered; until then, and after it +fails, the row shows the built-in 45, and Start sends whatever the row shows. +The endpoint accepts 10 to 90 either way, so a deployment that lowers the +default has not lowered the ceiling. A setting outside 10 to 90 is clamped and +named once on stderr when `codetrial web` or an agent mode starts, and text +that is no whole number refuses startup. + +`/api/session` reports the default as `defaultDurationMin`, clamped into 10 to +90; where recording is on, startup refuses a cap below that clamped length, as +the last paragraph says. The lobby preselects it for Medium and Hard, and +for Easy at most 30. A default that is not one of the three buttons selects the +nearest enabled one, and a length the candidate chose is never replaced by the +default. The Easy cap and the snapping are the lobby's suggestion only. +`/api/token` does not look at the problem: a request that names no length, such +as a link that skips the lobby, is granted `defaultDurationMin` itself, not +snapped, whatever the difficulty. A `durationMin` that is `null`, `false`, zero +or negative (`0`, `-5`), or text that is not a finite number (`abc`, +`Infinity`, `NaN`) names no length either, so it gets the same default and not +the built-in 45. A `duration` in +the interview URL that is not at least one whole minute (`0`, `0.5`, `-5`, +`30abc`, `Infinity`, `abc` or empty) names no length, so the interview page +sends none and gets the same default; a longer fraction is cut to whole +minutes, as the agent reads them. Recording lowers it. `CODETRIAL_RECORDING_MAX_MINUTES` bounds a single recording, and the sweeper fails one still running five minutes past it, so an @@ -18,14 +39,17 @@ offers; where it is off, the ceiling is 90. The lobby is told the ceiling rather than left to discover it. `/api/session` reports `maxDurationMin`, and the duration row disables the lengths above it -with the reason beside them — a length this deployment cannot record is never -offered and then quietly shortened. `/api/token` clamps to the same ceiling, so -a request that skips the lobby is bounded too, and both endpoints read it from +with the reason beside them — once the session has answered, a length this +deployment cannot record is never offered and then quietly shortened. A Start +pressed before it answers sends the 45 on screen, and `/api/token` shortens it +to the cap. `/api/token` clamps to the same ceiling, so a request that skips +the lobby is bounded too, and both endpoints read it from one function so the length that is offered and the length that is enforced cannot drift apart. -Startup refuses a cap below `CODETRIAL_DURATION_MIN`, so the length this -deployment preselects is always one it can record; the pairing is a -configuration error rather than a quietly shortened interview. Should a cap ever -sit below every length the row offers, the lobby leaves the row alone rather -than rendering a page with nothing to press. +Where recording is on, startup refuses a cap below `CODETRIAL_DURATION_MIN`, +held to 10 to 90 like the default itself, so the length this deployment +preselects is always one it can record; the pairing is a configuration error +rather than a quietly shortened interview. Should a cap ever sit below every +length the row offers, the lobby leaves the row alone rather than rendering a +page with nothing to press. diff --git a/src/config.rs b/src/config.rs index 2265e0ed2..e5fdb2feb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ use std::collections::BTreeMap; use std::fmt; +use std::num::IntErrorKind; use std::path::Path; mod peer_observer; @@ -443,6 +444,11 @@ pub fn load_from_pairs( invalid_entries.push("CODETRIAL_MAX_INTERIM_REVIEWS must be a whole number".into()); } + let default_duration_min = configured_duration(&values).unwrap_or_else(|message| { + invalid_entries.push(message); + (DEFAULT_DURATION_MIN, None) + }); + if !missing_keys.is_empty() || !invalid_entries.is_empty() { return Err(ConfigError { missing_keys, @@ -498,7 +504,7 @@ pub fn load_from_pairs( DEFAULT_GEMINI_END_SENSITIVITY, ), room_prefix: optional(&values, "CODETRIAL_ROOM_PREFIX", DEFAULT_ROOM_PREFIX), - default_duration_min: optional_u32(&values, "CODETRIAL_DURATION_MIN", DEFAULT_DURATION_MIN), + default_duration_min: default_duration_min.0, gemini_candidate_video_enabled: gemini_candidate_video_enabled( values .get("CODETRIAL_GEMINI_CANDIDATE_VIDEO_ENABLED") @@ -660,6 +666,43 @@ pub fn max_concurrent_interviews( } } +/// The length `CODETRIAL_DURATION_MIN` stands for, held to the range the +/// endpoints offer, and what to say when a number is clamped. Text that is no +/// number is refused rather than read as the built-in length: `3O` typed for 30 +/// would otherwise run a 45-minute default on a host nobody watches. One +/// function for both entry points, so the number in the message is the one the +/// server runs on. +pub fn configured_duration( + values: &BTreeMap, +) -> Result<(u32, Option), String> { + let value = present(values, "CODETRIAL_DURATION_MIN").unwrap_or_default(); + if value.is_empty() { + return Ok((DEFAULT_DURATION_MIN, None)); + } + + // A number past `i64` is still a number, only a very large or small one, so + // it is clamped like the rest. + let minutes = match value.parse::() { + Ok(minutes) => minutes, + Err(error) if *error.kind() == IntErrorKind::PosOverflow => i64::MAX, + Err(error) if *error.kind() == IntErrorKind::NegOverflow => i64::MIN, + Err(_) => { + return Err(format!( + "CODETRIAL_DURATION_MIN {value:?} must be a whole number of minutes" + )); + } + }; + let used = + u32::try_from(minutes.clamp(i64::from(MIN_DURATION_MIN), i64::from(MAX_DURATION_MIN))) + .unwrap_or(DEFAULT_DURATION_MIN); + let warning = (i64::from(used) != minutes).then(|| { + format!( + "codetrial: CODETRIAL_DURATION_MIN {value:?} is outside {MIN_DURATION_MIN} to {MAX_DURATION_MIN}; read as {used}" + ) + }); + Ok((used, warning)) +} + fn optional_u32(values: &BTreeMap, key: &str, default: u32) -> u32 { values .get(key) diff --git a/src/launch.rs b/src/launch.rs index ae77591e3..21caade1c 100644 --- a/src/launch.rs +++ b/src/launch.rs @@ -11,7 +11,7 @@ mod peer; pub use peer::{PeerServices, peer_services}; -use crate::config::{DEFAULT_DURATION_MIN, DEFAULT_ROOM_PREFIX, DEFAULT_WEB_DIR}; +use crate::config::{DEFAULT_ROOM_PREFIX, DEFAULT_WEB_DIR}; use crate::web::WebServerConfig; /// One installation keeps its config file and its account database together in @@ -60,10 +60,12 @@ pub fn web_server_config( // After the pool is complete, so the "is this project in the pool" check // sees the pool the server will actually serve, and before the listener // does any work: a half-configured recording block is a startup failure, - // never a surprise at the moment a candidate's interview ends. - let recording = - crate::config::load_recording(values, &pool, interview_duration_min(values), production) - .map_err(|error| error.to_string())?; + // never a surprise at the moment a candidate's interview ends. Read here + // rather than through `load_from_pairs`, because `codetrial web` + // deliberately runs without a full agent config. + let (default_duration_min, _) = crate::config::configured_duration(values)?; + let recording = crate::config::load_recording(values, &pool, default_duration_min, production) + .map_err(|error| error.to_string())?; recording_needs_verified_identity( recording.is_some(), nonempty(values, "GITHUB_CLIENT_ID").is_some() @@ -104,6 +106,7 @@ pub fn web_server_config( allowed_hosts: allowed_hosts(values)?, compiler_explorer_enabled: compiler_explorer_enabled(values), guest_mode_enabled: guest_mode_enabled(values), + default_duration_min, recording, peer_observer, peer, @@ -565,15 +568,6 @@ pub fn recording_needs_verified_identity(recording: bool, oauth: bool) -> Result Ok(()) } -/// The configured interview length, which is what a recording's maximum -/// duration has to clear. Read here rather than through `load_from_pairs` -/// because `codetrial web` deliberately runs without a full agent config. -fn interview_duration_min(values: &BTreeMap) -> u32 { - nonempty(values, "CODETRIAL_DURATION_MIN") - .and_then(|value| value.parse().ok()) - .unwrap_or(DEFAULT_DURATION_MIN) -} - /// `CODETRIAL_ALLOWED_HOSTS`, a comma-separated list of `Host` values. /// /// Unset or empty means no check. A value that holds only separators is @@ -841,6 +835,10 @@ pub fn prepare( let max_concurrent = read_max_concurrent(values); let production = crate::config::is_production(values); + + if let Some(warning) = crate::config::configured_duration(values)?.1 { + eprintln!("{warning}"); + } let pool = web_provider_pool(values, options, production)?; let config = web_server_config(values, pool, production, options)?; diff --git a/src/main.rs b/src/main.rs index 8c6268ee0..166575849 100644 --- a/src/main.rs +++ b/src/main.rs @@ -607,6 +607,13 @@ fn load_agent_config(options: &CliOptions) -> Result { let values = load_values(options)?; let production = is_production(&values); let provider_order = value_or(&values, codetrial::config::PROVIDER_ORDER_KEY, ""); + + // Said here and not in `load_from_pairs`, which `codetrial web` also calls + // after it has printed the same warning. A number that is no number is + // refused by `load_from_pairs` itself. + if let Ok((_, Some(warning))) = codetrial::config::configured_duration(&values) { + eprintln!("{warning}"); + } let mut config = codetrial::config::load_from_pairs(values).map_err(|error| error.to_string())?; extend_pool( diff --git a/src/web/auth.rs b/src/web/auth.rs index 6decca8ce..9366810f3 100644 --- a/src/web/auth.rs +++ b/src/web/auth.rs @@ -578,8 +578,10 @@ pub(crate) async fn session_handler( // The lobby offers lengths this deployment may not be able to record, and // only the server knows the recording cap. Answered on both branches // because the duration row is live before anyone signs in. - let max_duration_min = - super::token::duration_ceiling(state.config.recording.as_ref().map(|it| it.max_minutes)); + let recording_max_minutes = state.config.recording.as_ref().map(|it| it.max_minutes); + let max_duration_min = super::token::duration_ceiling(recording_max_minutes); + let default_duration_min = + super::token::default_duration(state.config.default_duration_min, recording_max_minutes); // Null on the web half of a split deployment, where the lobby keeps its // billing notice hidden rather than describe an agent it cannot see. @@ -605,6 +607,7 @@ pub(crate) async fn session_handler( "guestModeAvailable": state.guest_mode_available(), "deviceLogin": device_client_id(&state.config).is_some(), "maxDurationMin": max_duration_min, + "defaultDurationMin": default_duration_min, "candidateVideo": candidate_video, "user": { "login": user.login, @@ -626,6 +629,7 @@ pub(crate) async fn session_handler( "guestModeAvailable": state.guest_mode_available(), "deviceLogin": device_client_id(&state.config).is_some(), "maxDurationMin": max_duration_min, + "defaultDurationMin": default_duration_min, "candidateVideo": candidate_video, }), ), diff --git a/src/web/mod.rs b/src/web/mod.rs index 701abed4c..03ae4e267 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -92,6 +92,9 @@ pub struct WebServerConfig { pub compiler_explorer_enabled: bool, /// Guest interviews require explicit operator opt-in. pub guest_mode_enabled: bool, + /// `CODETRIAL_DURATION_MIN`: the lobby's preselected length, and the one + /// a token request that names none gets. + pub default_duration_min: u32, /// Number of trusted reverse proxies in front of this server. Zero, the /// default, keys rate limiting on the TCP peer. Anything higher reads the /// client from `X-Forwarded-For`, which is only safe once every request is @@ -180,6 +183,7 @@ impl fmt::Debug for WebServerConfig { production, compiler_explorer_enabled, guest_mode_enabled, + default_duration_min, trusted_proxy_hops, allowed_hosts, recording, @@ -212,6 +216,7 @@ impl fmt::Debug for WebServerConfig { .field("production", production) .field("compiler_explorer_enabled", compiler_explorer_enabled) .field("guest_mode_enabled", guest_mode_enabled) + .field("default_duration_min", default_duration_min) .field("trusted_proxy_hops", trusted_proxy_hops) .field("allowed_hosts", allowed_hosts) .field("recording", recording) diff --git a/src/web/token.rs b/src/web/token.rs index c16438967..13d66bd74 100644 --- a/src/web/token.rs +++ b/src/web/token.rs @@ -14,7 +14,7 @@ use axum::response::{IntoResponse, Response}; use ring::rand::SecureRandom; use serde_json::{Value, json}; -use crate::config::{DEFAULT_DURATION_MIN, MAX_DURATION_MIN, MIN_DURATION_MIN}; +use crate::config::{MAX_DURATION_MIN, MIN_DURATION_MIN}; use crate::current_epoch_seconds; use crate::token::{LivekitTokenInput, TOKEN_TTL_SECONDS, livekit_observer_token, livekit_token}; @@ -39,6 +39,8 @@ pub struct TokenConfig<'a> { /// recording/sweeper.rs reaps one still running past this many minutes, so /// a longer interview loses exactly the stretch it was recorded for. pub recording_max_min: Option, + /// `CODETRIAL_DURATION_MIN`: the length of a request that names none. + pub default_duration_min: u32, } /// Same reason, and the sharpest case of it: `token` is a minted LiveKit @@ -337,7 +339,11 @@ pub fn token_response( .get("problemId") .and_then(Value::as_str) .unwrap_or(crate::agent::DEFAULT_PROBLEM_ID); - let duration_min = token_duration_min(request.get("durationMin"), config.recording_max_min); + let duration_min = token_duration_min( + request.get("durationMin"), + config.default_duration_min, + config.recording_max_min, + ); let interview_loop = crate::agent::InterviewLoop::parse(request.get("interviewLoop").and_then(Value::as_str)); let interview_mode = @@ -459,17 +465,31 @@ pub(crate) fn duration_ceiling(recording_max_min: Option) -> u32 { }) } -pub(crate) fn token_duration_min(value: Option<&Value>, recording_max_min: Option) -> Value { +/// The operator's `CODETRIAL_DURATION_MIN` as the lobby and `/api/token` both +/// use it: inside the range the endpoint offers and no longer than the +/// deployment can record. One function for the same reason as +/// [`duration_ceiling`]. +pub(crate) fn default_duration(configured_min: u32, recording_max_min: Option) -> u32 { + configured_min + .clamp(MIN_DURATION_MIN, MAX_DURATION_MIN) + .min(duration_ceiling(recording_max_min)) +} + +pub(crate) fn token_duration_min( + value: Option<&Value>, + default_duration_min: u32, + recording_max_min: Option, +) -> Value { let ceiling = duration_ceiling(recording_max_min); let duration = value.and_then(crate::agent::json_number).unwrap_or(0.0); - if duration == 0.0 || duration.is_nan() { + if duration <= 0.0 || !duration.is_finite() { // The default is a request like any other where the cap is shorter than // it. Handing back a length this server cannot record, for a caller who // named no length at all, is the same lost stretch of interview. - json!(DEFAULT_DURATION_MIN.min(ceiling)) + json!(default_duration(default_duration_min, recording_max_min)) } else { - // Fractional minutes stay fractional: the metadata is echoed back to - // the frontend, which renders whatever the lobby asked for. + // Fractional minutes stay fractional for a caller that sends them; the + // interview page cuts to whole minutes before it asks. number_json(duration.clamp(MIN_DURATION_MIN.into(), ceiling.into())) } } @@ -766,6 +786,7 @@ fn minted_token( api_secret: &provider.api_secret, server_url: &provider.url, recording_max_min: state.config.recording.as_ref().map(|it| it.max_minutes), + default_duration_min: state.config.default_duration_min, }, body, room_name, diff --git a/tests/browser/account.test.js b/tests/browser/account.test.js index 2ea5df70f..d2d2a3564 100644 --- a/tests/browser/account.test.js +++ b/tests/browser/account.test.js @@ -229,7 +229,7 @@ test("the lobby offers an editor or a whiteboard and carries the choice into the ); assertIncludesCompact( interview, - "durationMin, interviewId, interviewLoop, interviewMode: mode, ...(!whiteboard ? { language: state.language } : {}), interviewProfile, ...(interviewGrounding", + "...(namedDuration ? { durationMin } : {}), interviewId, interviewLoop, interviewMode: mode, ...(!whiteboard ? { language: state.language } : {}), interviewProfile, ...(interviewGrounding", ); assertIncludesCompact( interview, diff --git a/tests/browser/interview-lifecycle.test.js b/tests/browser/interview-lifecycle.test.js index 22ef1d8b9..95efe119e 100644 --- a/tests/browser/interview-lifecycle.test.js +++ b/tests/browser/interview-lifecycle.test.js @@ -309,6 +309,7 @@ test("the actual connection preserves refusal reasons and gates the init deadlin updateRunAvailability() {}, tabStorage: {}, params: new URLSearchParams(), + namedDuration: false, durationMin: 45, codingMinutes: 45, behavioralMinutes: 0, diff --git a/tests/browser/interviewer-voice.test.js b/tests/browser/interviewer-voice.test.js index eac3ec58a..9986e4737 100644 --- a/tests/browser/interviewer-voice.test.js +++ b/tests/browser/interviewer-voice.test.js @@ -170,3 +170,151 @@ test("the interviewer controls fit narrow and desktop preflights", async (t) => await page.close(); } }); + +// A URL that skips the lobby names no length, so the server's configured +// default decides; sending the provisional 45 would override it. +for (const [query, sent] of [ + ["", false], + // Neither names a usable length, so neither may override the server default. + ["&duration=", false], + ["&duration=abc", false], + ["&duration=0", false], + ["&duration=-5", false], + // Under one whole minute once the fraction is cut, as the agent reads it. + ["&duration=0.5", false], + // Trailing text is not a number, so it names no length either, and neither + // is a number that is not finite. + ["&duration=30abc", false], + ["&duration=Infinity", false], + ["&duration=60", 60], + // The agent reads whole minutes, so the page asks for the same. + ["&duration=42.8", 42], +]) { + test(`the token request ${sent ? "names" : "omits"} the length for ${query || "a URL without duration"}`, async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await browser.newPage(); + try { + await page.route("**/api/token", (route) => + route.fulfill({ + status: 503, + contentType: "application/json", + body: JSON.stringify({ + error: "Token request captured by the test.", + }), + }), + ); + await page.goto( + `${base}/interview.html?problem=chargeback-pair-match${query}`, + { waitUntil: "domcontentloaded" }, + ); + await page.locator("#audio-heard").click(); + await page.locator("#camera-skip").click(); + await page.waitForFunction( + () => !document.querySelector("#audio-check-join").disabled, + ); + const request = page.waitForRequest((r) => + r.url().endsWith("/api/token"), + ); + await page.locator("#audio-check-join").click(); + const body = (await request).postDataJSON(); + assert.equal(Object.hasOwn(body, "durationMin"), sent !== false); + if (sent !== false) assert.equal(body.durationMin, sent); + } finally { + await page.close(); + } + }); +} + +// The round plan is drawn from the length, so a URL with no `duration` waits +// for the server's default instead of showing 45 minutes and jumping. +for (const query of ["", "&duration=", "&duration=abc"]) { + test(`the round plan for ?${query || "no duration"} waits for the server default`, async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await browser.newPage(); + try { + let release; + const held = new Promise((resolve) => (release = resolve)); + await page.route("**/api/session", async (route) => { + await held; + await route.fulfill({ + json: { signedIn: false, defaultDurationMin: 30, maxDurationMin: 90 }, + }); + }); + await page.goto( + `${base}/interview.html?problem=chargeback-pair-match&loop=coding_only${query}`, + { waitUntil: "domcontentloaded" }, + ); + const plan = page.locator("#round-plan-summary"); + // `renderProblem` writes the title in the same task as `bindEvents`; the + // static `#audio-check` is there before either has run. + await page.waitForFunction( + () => + document.querySelector("#problem-title").textContent !== + "Loading interview...", + ); + assert.equal(await plan.textContent(), ""); + release(); + await page.waitForFunction(() => + /30 minute coding budget/.test( + document.querySelector("#round-plan-summary").textContent, + ), + ); + } finally { + await page.close(); + } + }); +} + +// With no answer from the server either, the plan falls back to the 45 minutes +// a URL without a length shows. `-5` once read as 10 here, because the length +// shown was parsed apart from the decision that it named none. +for (const query of [ + "&duration=-5", + "&duration=0", + "&duration=30abc", + "&duration=Infinity", +]) { + test(`the round plan for ?${query} falls back to 45 when the session fails`, async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await browser.newPage(); + try { + await page.route("**/api/session", (route) => + route.fulfill({ status: 500, body: "nope" }), + ); + await page.goto( + `${base}/interview.html?problem=chargeback-pair-match&loop=coding_only${query}`, + { waitUntil: "domcontentloaded" }, + ); + await page.waitForFunction(() => + /45 minute coding budget/.test( + document.querySelector("#round-plan-summary").textContent, + ), + ); + } finally { + await page.close(); + } + }); +} + +test("the round plan for a URL with a duration is drawn at once", async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await browser.newPage(); + try { + await page.route("**/api/session", (route) => + route.fulfill({ + json: { signedIn: false, defaultDurationMin: 30, maxDurationMin: 90 }, + }), + ); + await page.goto( + `${base}/interview.html?problem=chargeback-pair-match&loop=coding_only&duration=60`, + { waitUntil: "domcontentloaded" }, + ); + await page.waitForFunction(() => + /60 minute coding budget/.test( + document.querySelector("#round-plan-summary").textContent, + ), + ); + } finally { + await page.close(); + } +}); diff --git a/tests/browser/lobby.test.js b/tests/browser/lobby.test.js index 13d09dd00..7bccdb71b 100644 --- a/tests/browser/lobby.test.js +++ b/tests/browser/lobby.test.js @@ -3770,7 +3770,7 @@ lobbyTest( ); lobbyTest( - "a candidate who touches nothing gets the server's own default length", + "an untouched lobby keeps the built-in length when the answer names none", async (page) => { const state = await lobby(page); @@ -3789,6 +3789,307 @@ lobbyTest( }, ); +for (const [configured, medium, easy] of [ + // The operator's own length replaces the built-in forty-five. + [30, "30", "30"], + [60, "60", "30"], + // Not a button: the nearest one is selected rather than none. + [20, "30", "30"], + [75, "60", "30"], + // Between two buttons the nearer one wins, not the next one up. + [35, "30", "30"], + [50, "45", "30"], +]) { + lobbyTest( + `an operator default of ${configured} minutes preselects ${medium}, or ${easy} for Easy`, + async (page) => { + session.defaultDurationMin = configured; + const state = await lobby(page); + assert.equal(state.duration, medium); + + await setLevel(page, "Easy", true); + await setLevel(page, "Medium", false); + assert.equal((await snapshot(page)).duration, easy); + }, + ); +} + +lobbyTest( + "a restored lobby suggests again after a level was ticked by hand", + async (page) => { + session.defaultDurationMin = 40; + session.maxDurationMin = 40; + await lobby(page); + // Ticking a level by hand skips the history-based suggestion on restore, + // so only the session can move the length now. + await setLevel(page, "Hard", true); + await setLevel(page, "Medium", false); + assert.equal((await snapshot(page)).duration, "30"); + + session.maxDurationMin = 60; + await page.evaluate(() => + window.dispatchEvent( + new PageTransitionEvent("pageshow", { persisted: true }), + ), + ); + await page.waitForFunction( + () => + document.querySelector("[data-duration].selected")?.dataset.duration === + "45", + ); + }, +); + +lobbyTest( + "a restored lobby leaves a length the candidate chose when the ceiling moves", + async (page) => { + session.maxDurationMin = 45; + await lobby(page); + await page.click('[data-duration="30"]'); + + session.maxDurationMin = 90; + await page.evaluate(() => + window.dispatchEvent( + new PageTransitionEvent("pageshow", { persisted: true }), + ), + ); + // The 60 button comes back only once the restored lobby has read the new + // ceiling, so the length below is checked after the session answered. + await page.waitForFunction( + () => !document.querySelector('[data-duration="60"]').disabled, + ); + assert.equal((await snapshot(page)).duration, "30"); + }, +); + +/// The session answers after the candidate has already acted, which is where +/// "left alone" has to hold: the latch is set before the default arrives. +const heldSession = async (page) => { + let release; + holdSession = new Promise((resolve) => (release = resolve)); + const requested = page.waitForRequest( + (request) => new URL(request.url()).pathname === "/api/session", + ); + await page.goto(`${base}/`, { waitUntil: "domcontentloaded" }); + await requested; + return async () => { + release(); + await page.waitForFunction( + () => document.querySelector("#recommendation").textContent !== "", + ); + }; +}; + +lobbyTest( + "an operator default does not overwrite a length chosen before the session answers", + async (page) => { + session.defaultDurationMin = 30; + const answer = await heldSession(page); + await page.click('[data-duration="60"]'); + await answer(); + assert.equal((await snapshot(page)).duration, "60"); + }, +); + +lobbyTest( + "an operator default does not replace a remembered length", + async (page) => { + session.defaultDurationMin = 60; + await page.addInitScript( + ([key, value]) => localStorage.setItem(key, value), + ["codetrial.interviewSettings", JSON.stringify({ durationMin: 45 })], + ); + assert.equal((await lobby(page)).duration, "45"); + }, +); + +lobbyTest( + "an operator default above the recording cap is moved down to the cap", + async (page) => { + // The server never sends this pair, since it holds the default to the cap + // itself; this is the lobby's own guard. + session.defaultDurationMin = 60; + session.maxDurationMin = 45; + assert.equal((await lobby(page)).duration, "45"); + }, +); + +lobbyTest( + "an operator default does not overrule a problem picked before the session answers", + async (page) => { + session.defaultDurationMin = 60; + const answer = await heldSession(page); + // Programmatic, because the picker is closed and the card is hidden. + await page.evaluate(() => + document.querySelector('[data-problem][data-difficulty="Easy"]').click(), + ); + await answer(); + assert.equal( + (await snapshot(page)).duration, + "30", + "the checkboxes still say Medium, which would suggest the operator's 60", + ); + }, +); + +lobbyTest( + "a start pressed before the session answers sends the length on screen", + async (page) => { + session.defaultDurationMin = 60; + let release; + holdSession = new Promise((resolve) => (release = resolve)); + // The navigation is held so the lobby that pressed Start can still be + // watched when the late answer lands; the page reports through console. + let leave; + const leaving = new Promise((resolve) => (leave = resolve)); + await page.route( + (url) => url.pathname === "/interview", + async (route) => { + await leaving; + await route.continue(); + }, + ); + const rows = []; + let applied; + const answered = new Promise((resolve) => (applied = resolve)); + page.on("console", (message) => { + const [kind, value] = message.text().split(" "); + if (kind === "row") rows.push(value); + if (kind === "status") applied(); + }); + const requested = page.waitForRequest( + (request) => new URL(request.url()).pathname === "/api/session", + ); + await page.goto(`${base}/`, { waitUntil: "domcontentloaded" }); + await requested; + // Programmatic, because the picker is closed and the card is hidden. + await page.evaluate(() => + document + .querySelector('[data-problem][data-difficulty="Medium"]') + .click(), + ); + await page.evaluate(() => { + const one = (selector) => document.querySelector(selector); + new MutationObserver(() => + console.log( + `row ${one(".duration-button.selected")?.dataset.duration}`, + ), + ).observe(document.body, { + attributes: true, + subtree: true, + attributeFilter: ["class"], + }); + // The account status is written by the answer, after the length. + new MutationObserver(() => console.log("status")).observe( + one("#account-status"), + { childList: true, characterData: true, subtree: true }, + ); + one("#start").click(); + }); + release(); + await answered; + leave(); + + await page.waitForURL(/\/interview/); + assert.deepEqual( + rows.filter((row) => row !== "45"), + [], + "the late answer moved the row under a start already on its way", + ); + assert.equal(new URL(page.url()).searchParams.get("duration"), "45"); + }, +); + +lobbyTest( + "a start pressed before the session answers sends a length the lobby changed", + async (page) => { + session.defaultDurationMin = 60; + let release; + holdSession = new Promise((resolve) => (release = resolve)); + const requested = page.waitForRequest( + (request) => new URL(request.url()).pathname === "/api/session", + ); + await page.goto(`${base}/`, { waitUntil: "domcontentloaded" }); + await requested; + // An Easy problem makes the row say 30, which the candidate saw, so the + // server must grant 30 and not the 60 it would pick for a request that + // names nothing. + await page.evaluate(() => + document.querySelector('[data-problem][data-difficulty="Easy"]').click(), + ); + await page.click("#start"); + release(); + + await page.waitForURL(/\/interview/); + assert.equal(new URL(page.url()).searchParams.get("duration"), "30"); + }, +); + +lobbyTest( + "a start pressed before the session answers sends a length the candidate clicked", + async (page) => { + session.defaultDurationMin = 60; + let release; + holdSession = new Promise((resolve) => (release = resolve)); + const requested = page.waitForRequest( + (request) => new URL(request.url()).pathname === "/api/session", + ); + await page.goto(`${base}/`, { waitUntil: "domcontentloaded" }); + await requested; + // Programmatic, because the picker is closed and the card is hidden. + await page.evaluate(() => + document + .querySelector('[data-problem][data-difficulty="Medium"]') + .click(), + ); + await page.click('[data-duration="60"]'); + await page.click("#start"); + release(); + + await page.waitForURL(/\/interview/); + assert.equal(new URL(page.url()).searchParams.get("duration"), "60"); + }, +); + +lobbyTest( + "a start after the session failed sends the length on screen", + async (page) => { + session = null; + failing = new Set(["/api/session"]); + await page.goto(`${base}/`, { waitUntil: "domcontentloaded" }); + await awaitReady(page); + await page.evaluate(() => + document + .querySelector('[data-problem][data-difficulty="Medium"]') + .click(), + ); + await page.click("#start"); + + await page.waitForURL(/\/interview/); + assert.equal(new URL(page.url()).searchParams.get("duration"), "45"); + }, +); + +lobbyTest( + "a start after the session failed sends a length the candidate clicked", + async (page) => { + session = null; + failing = new Set(["/api/session"]); + await page.goto(`${base}/`, { waitUntil: "domcontentloaded" }); + await awaitReady(page); + await page.evaluate(() => + document + .querySelector('[data-problem][data-difficulty="Medium"]') + .click(), + ); + await page.click('[data-duration="60"]'); + await page.click("#start"); + + await page.waitForURL(/\/interview/); + assert.equal(new URL(page.url()).searchParams.get("duration"), "60"); + }, +); + lobbyTest( "the recommendation does not move once it is on screen", async (page) => { diff --git a/tests/cli.rs b/tests/cli.rs index 326ceb8ee..57b265c67 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1029,30 +1029,6 @@ fn binary_reads_a_config_file_beside_the_executable() { ); } -/// Every mode that reads configuration refuses without a file, and says which -/// file it wanted. One test over the modes rather than one test each: the -/// assertion is the same sentence three times, and a mode added to MODES that -/// forgets this belongs in this list rather than in a fourth copy. -/// -/// `web` is not here: a missing config is a cold start for it, covered by -/// `binary_web_serves_a_setup_page_when_no_config_exists`. -#[test] -fn binary_modes_that_read_configuration_require_a_primary_config_file() { - for args in [ - &["run-livekit", "interview-fixed"][..], - &["check-gemini"][..], - ] { - let (code, stdout, stderr) = run_cli_args(args); - - assert_eq!(code, 1, "{args:?}: {stderr}"); - assert!(stdout.is_empty(), "{args:?}: {stdout}"); - assert!( - stderr.contains("required configuration file is missing"), - "{args:?}: {stderr}" - ); - } -} - /// `--config` alone still leaves zero positionals but skips the cold-start /// path, so the credentials refusal proves `web` was picked, not /// `run-livekit`/`check-gemini`. @@ -3169,3 +3145,6 @@ fn a_server_told_to_follow_its_stdin_exits_when_it_closes() { #[path = "cli/setup_save.rs"] mod setup_save; + +#[path = "cli/agent_modes.rs"] +mod agent_modes; diff --git a/tests/cli/agent_modes.rs b/tests/cli/agent_modes.rs new file mode 100644 index 000000000..f641c1b04 --- /dev/null +++ b/tests/cli/agent_modes.rs @@ -0,0 +1,66 @@ +//! How the binary's modes read their configuration. +//! +//! Split out of `tests/cli.rs`, which is still the test target. + +use super::*; + +/// Every mode that reads configuration refuses without a file, and says which +/// file it wanted. One test over the modes rather than one test each: the +/// assertion is the same sentence three times, and a mode added to MODES that +/// forgets this belongs in this list rather than in a fourth copy. +/// +/// `web` is not here: a missing config is a cold start for it, covered by +/// `binary_web_serves_a_setup_page_when_no_config_exists`. +#[test] +fn binary_modes_that_read_configuration_require_a_primary_config_file() { + for args in [ + &["run-livekit", "interview-fixed"][..], + &["check-gemini"][..], + ] { + let (code, stdout, stderr) = run_cli_args(args); + + assert_eq!(code, 1, "{args:?}: {stderr}"); + assert!(stdout.is_empty(), "{args:?}: {stdout}"); + assert!( + stderr.contains("required configuration file is missing"), + "{args:?}: {stderr}" + ); + } +} + +/// A length outside 10 to 90 is named once at startup, by `codetrial web` and +/// by the agent modes alike, as `docs/interview-length.md` says: `check-gemini` +/// would otherwise boot its session on 90 without a word, and a second reader +/// that printed it too would say it twice. +#[test] +fn binary_modes_name_a_duration_that_was_clamped_once() { + let dir = temp_path("duration-clamped"); + std::fs::create_dir_all(&dir).unwrap(); + let config = dir.join("codetrial.env.local"); + std::fs::write( + &config, + "CODETRIAL_DURATION_MIN=500\nCODETRIAL_WEB_ADDR=127.0.0.1:1\n", + ) + .unwrap(); + let runs: Vec<_> = ["web", "check-gemini"] + .into_iter() + .map(|mode| { + ( + mode, + run_cli_args(&[mode, "--config", config.to_str().unwrap()]), + ) + }) + .collect(); + let _ = std::fs::remove_dir_all(dir); + + for (mode, (code, _, stderr)) in runs { + assert_eq!(code, 1, "{mode}: {stderr}"); + assert_eq!( + stderr + .matches("CODETRIAL_DURATION_MIN \"500\" is outside 10 to 90; read as 90") + .count(), + 1, + "{mode}: {stderr}" + ); + } +} diff --git a/tests/unit/bin.rs b/tests/unit/bin.rs index 0b97db9d2..838bd594f 100644 --- a/tests/unit/bin.rs +++ b/tests/unit/bin.rs @@ -491,6 +491,59 @@ bm90IGEga2V5 assert!(super::recording_can_deliver(Some(&recording)).is_err()); } +fn recording_values(pairs: &[(&str, &str)]) -> super::BTreeMap { + let mut values: std::collections::BTreeMap = [ + ("CODETRIAL_RECORDING_ENABLED", "true"), + ("CODETRIAL_RECORDING_GCS_BUCKET", "codetrial-staging"), + ("CODETRIAL_RECORDING_DRIVE_ID", "0AKfixtureDriveId"), + ( + "CODETRIAL_RECORDING_SERVICE_ACCOUNT_JSON", + r#"{"type":"service_account","client_email":"a@b.iam.gserviceaccount.com","private_key":"KEYMATERIAL-4bd2"}"#, + ), + ( + "CODETRIAL_RECORDING_TEMPLATE_BASE_URL", + "https://recording.codetrial.example", + ), + ("SESSION_SECRET", "a-real-secret"), + ] + .into_iter() + .map(|(key, value)| (key.to_string(), value.to_string())) + .collect(); + for (key, value) in pairs { + values.insert(key.to_string(), value.to_string()); + } + values +} + +/// The recording check sees the length the endpoints grant, not the raw text: +/// `120` is served as 90 so a cap of 90 holds it, and a length left empty is +/// served as 45 so a cap below that is refused. +#[test] +fn the_recording_cap_is_checked_against_the_clamped_length() { + let pool = codetrial::config::ProviderPool { + providers: vec![provider(codetrial::config::PRIMARY_PROVIDER_ID)], + }; + let refusal = |duration: &str, cap: &str| { + let values = recording_values(&[ + ("CODETRIAL_DURATION_MIN", duration), + ("CODETRIAL_RECORDING_MAX_MINUTES", cap), + ]); + super::web_server_config(&values, pool.clone(), false, &super::CliOptions::default()).err() + }; + + // Past the cap check the fixture still lacks an OAuth app, so what matters + // is only whether the refusal is about the cap. + let about_the_cap = |error: &Option| { + error + .as_deref() + .is_some_and(|text| text.contains("CODETRIAL_RECORDING_MAX_MINUTES")) + }; + assert!(!about_the_cap(&refusal("120", "90"))); + assert!(refusal("120", "89").unwrap().contains("89, below the 90")); + assert!(refusal("", "44").unwrap().contains("44, below the 45")); + assert!(!about_the_cap(&refusal("", "45"))); +} + /// Half an OAuth app is not an OAuth app. /// /// The rule below is tested on its own; this is the wiring that feeds it, @@ -501,29 +554,6 @@ bm90IGEga2V5 /// half alone has to be refused, and so does neither. #[test] fn recording_starts_only_with_both_halves_of_the_oauth_app() { - let recording_values = |pairs: &[(&str, &str)]| { - let mut values: std::collections::BTreeMap = [ - ("CODETRIAL_RECORDING_ENABLED", "true"), - ("CODETRIAL_RECORDING_GCS_BUCKET", "codetrial-staging"), - ("CODETRIAL_RECORDING_DRIVE_ID", "0AKfixtureDriveId"), - ( - "CODETRIAL_RECORDING_SERVICE_ACCOUNT_JSON", - r#"{"type":"service_account","client_email":"a@b.iam.gserviceaccount.com","private_key":"KEYMATERIAL-4bd2"}"#, - ), - ( - "CODETRIAL_RECORDING_TEMPLATE_BASE_URL", - "https://recording.codetrial.example", - ), - ("SESSION_SECRET", "a-real-secret"), - ] - .into_iter() - .map(|(key, value)| (key.to_string(), value.to_string())) - .collect(); - for (key, value) in pairs { - values.insert(key.to_string(), value.to_string()); - } - values - }; let pool = codetrial::config::ProviderPool { providers: vec![provider(codetrial::config::PRIMARY_PROVIDER_ID)], }; @@ -673,6 +703,39 @@ fn a_data_dir_decides_every_path_a_launch_reads_or_writes() { ); } +/// The operator's `CODETRIAL_DURATION_MIN` is what the lobby and `/api/token` +/// default to, so it has to reach the web config. +#[test] +fn web_server_config_carries_the_configured_default_length() { + let options = super::CliOptions::default(); + let pool = codetrial::config::ProviderPool { + providers: vec![provider(codetrial::config::PRIMARY_PROVIDER_ID)], + }; + let configured = |values: &super::BTreeMap| { + super::web_server_config(values, pool.clone(), false, &options) + .expect("a plain local config should be accepted") + .default_duration_min + }; + assert_eq!( + configured(&values(&[])), + codetrial::config::DEFAULT_DURATION_MIN + ); + assert_eq!(configured(&values(&[("CODETRIAL_DURATION_MIN", "30")])), 30); + assert_eq!( + configured(&values(&[("CODETRIAL_DURATION_MIN", " 30 ")])), + 30 + ); + // A length typed as text is refused, not read as the built-in one. + let error = super::web_server_config( + &values(&[("CODETRIAL_DURATION_MIN", "3O")]), + pool.clone(), + false, + &options, + ) + .expect_err("a length that is no number must not start the server"); + assert!(error.contains("CODETRIAL_DURATION_MIN"), "{error}"); +} + /// Made once and kept, so a relaunch keeps everyone signed in, and owner-only /// because any program on the machine can reach a loopback port. #[test] diff --git a/tests/unit/dispatch_http.rs b/tests/unit/dispatch_http.rs index f6596e456..fd2531144 100644 --- a/tests/unit/dispatch_http.rs +++ b/tests/unit/dispatch_http.rs @@ -239,6 +239,7 @@ impl Probe { production: !pinned, compiler_explorer_enabled: false, guest_mode_enabled: true, + default_duration_min: crate::config::DEFAULT_DURATION_MIN, trusted_proxy_hops: 1, allowed_hosts: Vec::new(), recording: None, diff --git a/tests/unit/launch.rs b/tests/unit/launch.rs index 280091233..a528ffbe8 100644 --- a/tests/unit/launch.rs +++ b/tests/unit/launch.rs @@ -85,10 +85,15 @@ fn the_launch_reads_its_cap_length_and_compiler_switch_from_the_values() { 5 ); assert_eq!( - interview_duration_min(&values(&[("CODETRIAL_DURATION_MIN", "45")])), + crate::config::configured_duration(&values(&[("CODETRIAL_DURATION_MIN", "45")])) + .unwrap() + .0, 45 ); - assert_eq!(interview_duration_min(&values(&[])), DEFAULT_DURATION_MIN); + assert_eq!( + crate::config::configured_duration(&values(&[])).unwrap().0, + crate::config::DEFAULT_DURATION_MIN + ); for (value, enabled) in [("true", true), ("false", false)] { assert_eq!( compiler_explorer_enabled(&values(&[("CODETRIAL_COMPILER_EXPLORER_ENABLED", value)])), @@ -96,3 +101,81 @@ fn the_launch_reads_its_cap_length_and_compiler_switch_from_the_values() { ); } } + +/// The length a launch runs on and what the operator is told about it come from +/// one function, so the number in a warning is the one the server uses, and the +/// recording check sees the same clamped length the endpoints grant. +#[test] +fn a_duration_setting_is_read_once_and_named_when_not_used_as_written() { + let values = |pairs: &[(&str, &str)]| -> BTreeMap { + pairs + .iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect() + }; + let read = |value: &str| { + let pairs = values(&[("CODETRIAL_DURATION_MIN", value)]); + crate::config::configured_duration(&pairs) + }; + let agent = |value: &str| { + crate::config::load_from_pairs([ + ("LIVEKIT_URL", "wss://primary.example"), + ("LIVEKIT_API_KEY", "primary-key"), + ("LIVEKIT_API_SECRET", "primary-secret"), + ("GOOGLE_API_KEY", "primary-google"), + ("CODETRIAL_DURATION_MIN", value), + ]) + }; + let unset = values(&[]); + assert_eq!( + crate::config::configured_duration(&unset), + Ok((crate::config::DEFAULT_DURATION_MIN, None)) + ); + for (value, used) in [("", 45), (" ", 45), ("10", 10), (" 60 ", 60), ("90", 90)] { + assert_eq!(read(value), Ok((used, None)), "{value:?}"); + } + for (value, used) in [ + ("-30", 10), + ("0", 10), + ("5", 10), + ("91", 90), + ("120", 90), + ("500", 90), + ("4294967296", 90), + ("99999999999999999999", 90), + ("-99999999999999999999", 10), + ("+99999999999999999999", 90), + ] { + let (length, said) = read(value).unwrap(); + assert_eq!(length, used, "{value:?}"); + let said = said.unwrap_or_else(|| panic!("{value:?} was accepted silently")); + assert!(said.contains(value), "{said}"); + assert!(said.ends_with(&format!("read as {used}")), "{said}"); + + // The agent path (`check-gemini`, `run-livekit`) reads the same length. + assert_eq!( + agent(value).unwrap().default_duration_min, + used, + "{value:?}" + ); + } + + // Text that is no number is refused on both paths, since reading `3O` as + // the built-in 45 would quietly run the wrong length on a host nobody + // watches. + for value in ["3O", "30min", "45.5", "abc", "-", "+", "1e2"] { + let error = read(value).expect_err(value); + assert!(error.contains("CODETRIAL_DURATION_MIN"), "{error}"); + assert!(error.contains(value), "{error}"); + let refused = agent(value) + .err() + .unwrap_or_else(|| panic!("{value:?} was accepted by the agent")); + assert!( + refused + .invalid_entries + .iter() + .any(|entry| entry.contains("CODETRIAL_DURATION_MIN")), + "{value:?}: {refused}" + ); + } +} diff --git a/tests/unit/livekit/recovery_live_parts/launcher_and_scenario_judging.rs b/tests/unit/livekit/recovery_live_parts/launcher_and_scenario_judging.rs index b84781ae8..601f36f29 100644 --- a/tests/unit/livekit/recovery_live_parts/launcher_and_scenario_judging.rs +++ b/tests/unit/livekit/recovery_live_parts/launcher_and_scenario_judging.rs @@ -24,6 +24,7 @@ impl<'a> LiveEffects<'a> { api_secret: &config.livekit_api_secret, server_url: &config.livekit_url, recording_max_min: recording_cap, + default_duration_min: crate::config::DEFAULT_DURATION_MIN, }, body.as_bytes(), &room_name, diff --git a/tests/unit/livekit/recovery_live_parts/report_generation_cardinality.rs b/tests/unit/livekit/recovery_live_parts/report_generation_cardinality.rs index 6376ae482..8a6310d6f 100644 --- a/tests/unit/livekit/recovery_live_parts/report_generation_cardinality.rs +++ b/tests/unit/livekit/recovery_live_parts/report_generation_cardinality.rs @@ -245,6 +245,7 @@ impl G2NativeRun { api_key: &self.config.livekit_api_key, api_secret: &self.config.livekit_api_secret, server_url: &self.config.livekit_url, + default_duration_min: crate::config::DEFAULT_DURATION_MIN, recording_max_min: None, }, br#"{"problemId":"two-sum","durationMin":10}"#, diff --git a/tests/unit/web/policy.rs b/tests/unit/web/policy.rs index a84640de3..28ec7820b 100644 --- a/tests/unit/web/policy.rs +++ b/tests/unit/web/policy.rs @@ -25,6 +25,7 @@ fn policy_for(url: &str) -> String { production: true, compiler_explorer_enabled: false, guest_mode_enabled: false, + default_duration_min: crate::config::DEFAULT_DURATION_MIN, trusted_proxy_hops: 0, allowed_hosts: Vec::new(), recording: None, diff --git a/tests/unit/web/pool.rs b/tests/unit/web/pool.rs index dd9caa1ed..4d0e3c9c6 100644 --- a/tests/unit/web/pool.rs +++ b/tests/unit/web/pool.rs @@ -32,6 +32,7 @@ fn config_with(ids: &[&str]) -> WebServerConfig { production: false, compiler_explorer_enabled: false, guest_mode_enabled: false, + default_duration_min: crate::config::DEFAULT_DURATION_MIN, trusted_proxy_hops: 0, allowed_hosts: Vec::new(), recording: None, diff --git a/tests/unit/web/setup_edit.rs b/tests/unit/web/setup_edit.rs index a09fa6c82..3de9ed5cf 100644 --- a/tests/unit/web/setup_edit.rs +++ b/tests/unit/web/setup_edit.rs @@ -252,6 +252,7 @@ impl Server { production: false, compiler_explorer_enabled: false, guest_mode_enabled: true, + default_duration_min: crate::config::DEFAULT_DURATION_MIN, trusted_proxy_hops: 0, allowed_hosts: Vec::new(), recording: None, diff --git a/tests/web.rs b/tests/web.rs index affb2fe8a..8593ebca7 100644 --- a/tests/web.rs +++ b/tests/web.rs @@ -137,6 +137,7 @@ fn web_config() -> WebServerConfig { production: false, compiler_explorer_enabled: true, guest_mode_enabled: true, + default_duration_min: DEFAULT_DURATION_MIN, trusted_proxy_hops: 0, allowed_hosts: Vec::new(), recording: None, @@ -1396,6 +1397,9 @@ async fn spawn_counting_quota_stub( #[path = "web/token.rs"] mod token; +#[path = "web/token_default.rs"] +mod token_default; + #[path = "web/policy.rs"] mod policy; diff --git a/tests/web/accounts.rs b/tests/web/accounts.rs index a6ca2e5c5..e47912c68 100644 --- a/tests/web/accounts.rs +++ b/tests/web/accounts.rs @@ -129,7 +129,7 @@ async fn account_routes_record_interviewee_github_login() { assert_eq!( session.json::().await.unwrap(), json!({"signedIn": false, "loginRequired": true, - "guestModeAvailable": true, "deviceLogin": false, "maxDurationMin": MAX_DURATION_MIN, + "guestModeAvailable": true, "deviceLogin": false, "maxDurationMin": MAX_DURATION_MIN, "defaultDurationMin": DEFAULT_DURATION_MIN, "peerEnabled": false, "peerMaxRoundMinutes": null, "peerObserverAvailable": false, "peerAudioAvailable": false, "candidateVideo": null}) ); @@ -329,7 +329,7 @@ async fn an_unmigratable_account_database_refuses_rather_than_disabling_login() assert_eq!( session, json!({"signedIn": false, "loginRequired": true, - "guestModeAvailable": false, "deviceLogin": false, "maxDurationMin": MAX_DURATION_MIN, + "guestModeAvailable": false, "deviceLogin": false, "maxDurationMin": MAX_DURATION_MIN, "defaultDurationMin": DEFAULT_DURATION_MIN, "peerEnabled": false, "peerMaxRoundMinutes": null, "peerObserverAvailable": false, "peerAudioAvailable": false, "candidateVideo": null}), "a database this binary cannot migrate still requires a login" ); @@ -371,7 +371,7 @@ async fn an_unopenable_account_database_refuses_rather_than_disabling_login() { assert_eq!( session, json!({"signedIn": false, "loginRequired": true, - "guestModeAvailable": false, "deviceLogin": false, "maxDurationMin": MAX_DURATION_MIN, + "guestModeAvailable": false, "deviceLogin": false, "maxDurationMin": MAX_DURATION_MIN, "defaultDurationMin": DEFAULT_DURATION_MIN, "peerEnabled": false, "peerMaxRoundMinutes": null, "peerObserverAvailable": false, "peerAudioAvailable": false, "candidateVideo": null}), "a broken database still requires a login; it cannot serve one" ); @@ -428,7 +428,7 @@ async fn an_unopenable_account_database_refuses_rather_than_disabling_login() { "signedIn": false, "loginRequired": false, "guestModeAvailable": true, "deviceLogin": false, - "maxDurationMin": MAX_DURATION_MIN, + "maxDurationMin": MAX_DURATION_MIN, "defaultDurationMin": DEFAULT_DURATION_MIN, "peerEnabled": false, "peerMaxRoundMinutes": null, "peerObserverAvailable": false, "peerAudioAvailable": false, "candidateVideo": null }), "nothing to sign in to, and the lobby has to be told so" @@ -1076,7 +1076,7 @@ async fn account_reports_are_scoped_to_the_signed_in_user() { assert_eq!( signed_out, json!({"signedIn": false, "loginRequired": true, - "guestModeAvailable": true, "deviceLogin": false, "maxDurationMin": MAX_DURATION_MIN, + "guestModeAvailable": true, "deviceLogin": false, "maxDurationMin": MAX_DURATION_MIN, "defaultDurationMin": DEFAULT_DURATION_MIN, "peerEnabled": false, "peerMaxRoundMinutes": null, "peerObserverAvailable": false, "peerAudioAvailable": false, "candidateVideo": null}), "accounts exist here, so the browser has to know to demand a sign-in" ); diff --git a/tests/web/contract.rs b/tests/web/contract.rs index 75c11febd..60f14932b 100644 --- a/tests/web/contract.rs +++ b/tests/web/contract.rs @@ -59,6 +59,7 @@ async fn static_home_markup_matches_frontend_contract() { production: false, compiler_explorer_enabled: true, guest_mode_enabled: false, + default_duration_min: DEFAULT_DURATION_MIN, trusted_proxy_hops: 0, allowed_hosts: Vec::new(), recording: None, @@ -121,6 +122,7 @@ async fn static_interview_markup_exposes_offline_surface() { production: false, compiler_explorer_enabled: true, guest_mode_enabled: false, + default_duration_min: DEFAULT_DURATION_MIN, trusted_proxy_hops: 0, allowed_hosts: Vec::new(), recording: None, @@ -299,7 +301,7 @@ fn static_interview_script_leaves_candidate_identity_to_the_server() { // The shared request fields follow the mode-specific problem selection. // Strict assignment must retain the optional execution restriction too. assert!(compact(&source).contains( - "durationMin,interviewId,interviewLoop,interviewMode:mode,...(!whiteboard?{language:state.language}:{}),interviewProfile,...(interviewGrounding?{interviewGrounding}:{}),...(nodes.interviewerAccent.value?{interviewerAccent:nodes.interviewerAccent.value}:{}),...(nodes.interviewerVoice.value?{interviewerVoice:nodes.interviewerVoice.value}:{}),...(nodes.hideExamples.checked?{hideExamples:true}:{}),...(exampleDrawingEnabled?{exampleDrawing:true}:{}),...(candidateLedEnabled?{candidateLed:true}:{}),...(!editorOptions.execution?{codeExecution:false}:{})})" + "...(namedDuration?{durationMin}:{}),interviewId,interviewLoop,interviewMode:mode,...(!whiteboard?{language:state.language}:{}),interviewProfile,...(interviewGrounding?{interviewGrounding}:{}),...(nodes.interviewerAccent.value?{interviewerAccent:nodes.interviewerAccent.value}:{}),...(nodes.interviewerVoice.value?{interviewerVoice:nodes.interviewerVoice.value}:{}),...(nodes.hideExamples.checked?{hideExamples:true}:{}),...(exampleDrawingEnabled?{exampleDrawing:true}:{}),...(candidateLedEnabled?{candidateLed:true}:{}),...(!editorOptions.execution?{codeExecution:false}:{})})" )); assert!(!source.contains("candidateIdentity")); } @@ -1201,11 +1203,13 @@ fn browser_interview_duration_matches_the_server_clamp() { "web/interview.js clamps above the server maximum" ); - // The fallback the query string gets when it carries no duration. - let fallback = clamp[0] - .rsplit("||") + // The fallback the query string gets when it names no usable duration: the + // literal after the colon of `namedDuration ? clamp(...) : `. + let declaration = &interview[interview.find("let durationMin = ").unwrap()..]; + let fallback = declaration[..declaration.find(';').unwrap()] + .rsplit(':') .next() - .expect("the clamped value falls back to a literal") + .expect("the declaration falls back to a literal") .trim(); assert_eq!( parsed(fallback), @@ -1231,7 +1235,8 @@ fn browser_interview_duration_matches_the_server_clamp() { } assert!(!offered.is_empty(), "the lobby offers no durations at all"); - // The preselected button decides what a candidate who touches nothing gets. + // The preselected button is what a candidate who touches nothing sees until + // `/api/session` names the operator's default. let selected = page .split("duration-button selected\"") .nth(1) @@ -1239,7 +1244,7 @@ fn browser_interview_duration_matches_the_server_clamp() { assert_eq!( browser_number(selected, "data-duration=\"", '"'), DEFAULT_DURATION_MIN, - "the preselected lobby duration is not the server default" + "the preselected lobby duration is not the built-in default" ); // The lobby derives its length from the checked difficulty, so the two @@ -1247,8 +1252,8 @@ fn browser_interview_duration_matches_the_server_clamp() { // checked: two would make the derived length ambiguous, and none would // leave `suggestedDuration` deciding from an empty set. // - // What that box derives is checked in the browser rather than here, by "a - // candidate who touches nothing gets the server's own default length" in + // What that box derives is checked in the browser rather than here, by "an + // untouched lobby keeps the built-in length when the answer names none" in // tests/browser/lobby.test.js, which asserts that the length app.js renders // on an untouched lobby is the preselected one this test just pinned to // DEFAULT_DURATION_MIN. There is no longer a lobby-side literal to read: diff --git a/tests/web/routes.rs b/tests/web/routes.rs index d9d301287..ccbae9cf7 100644 --- a/tests/web/routes.rs +++ b/tests/web/routes.rs @@ -259,6 +259,44 @@ async fn the_oauth_callback_requires_the_state_it_minted() { remove_database(path).await; } +/// The lobby preselects the operator's length, so the server has to say what +/// it is; the page cannot read `CODETRIAL_DURATION_MIN` itself. What it says is +/// the length `/api/token` would grant: inside the range, and no longer than +/// the recording cap. +#[tokio::test] +async fn the_session_reports_the_default_length_it_would_grant() { + for (configured, recording, expected) in [ + (30, None, 30), + (5, None, MIN_DURATION_MIN), + (120, None, MAX_DURATION_MIN), + ( + 120, + Some(recording_config()), + recording_config().max_minutes, + ), + ] { + let mut config = web_config(); + config.default_duration_min = configured; + config.recording = recording; + let (base, server) = spawn_web_server(config).await; + + let session = http_client() + .get(format!("{base}/api/session")) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + + assert_eq!( + session["defaultDurationMin"], expected, + "configured {configured}" + ); + server.shutdown().await; + } +} + /// The same endpoint on a server that records nothing, so the cap is the range /// the endpoint has always offered rather than a number the lobby invents. #[tokio::test] diff --git a/tests/web/token.rs b/tests/web/token.rs index 7f3f97166..0707120e9 100644 --- a/tests/web/token.rs +++ b/tests/web/token.rs @@ -103,6 +103,7 @@ fn token_response_matches_frontend_contract() { api_secret: "devsecret", server_url: "wss://example.livekit.cloud", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }, br#"{"problemId":"merge-intervals","durationMin":120}"#, "interview-fixed", @@ -136,6 +137,7 @@ fn token_ignores_a_mode_a_stale_client_still_sends() { api_secret: "devsecret", server_url: "wss://example.livekit.cloud", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }; for body in [ br#"{"mode":"practice"}"#.as_slice(), @@ -163,6 +165,7 @@ fn token_interview_mode_is_allowlisted_and_defaults_to_the_editor() { api_secret: "secret", server_url: "wss://example.test", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }; for (body, expected) in [ ( @@ -192,6 +195,7 @@ fn token_interview_loop_is_allowlisted_and_defaults_to_combined() { api_secret: "secret", server_url: "wss://example.test", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }; for (body, expected) in [ ( @@ -218,6 +222,7 @@ fn only_explicit_false_disables_execution_in_signed_metadata() { api_secret: "secret", server_url: "wss://example.test", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }; for value in [ json!(false), @@ -263,6 +268,7 @@ fn token_profile_is_bounded_and_enum_validated_before_signed_metadata() { api_secret: "secret", server_url: "wss://example.test", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }, serde_json::to_string(&json!({"interviewProfile": { "role": format!(" {}\n", "r".repeat(100)), @@ -300,6 +306,7 @@ fn token_profile_is_bounded_and_enum_validated_before_signed_metadata() { api_secret: "secret", server_url: "wss://example.test", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }, br#"{"interviewProfile":{"seniority":"founder","role":4}}"#, "room", @@ -320,6 +327,7 @@ fn token_grounding_is_signed_only_after_valid_consent_and_shape() { api_secret: "secret", server_url: "wss://example.test", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }; let signed = token_response(&config, br#"{"interviewGrounding":{"consentVersion":1,"requirements":["Must know Rust"],"skills":["Rust"],"anchors":["Built a parser"]}}"#, "room", "candidate", 2_000).unwrap(); let metadata: Value = @@ -350,6 +358,7 @@ fn token_carries_hidden_examples_only_when_true() { api_secret: "secret", server_url: "wss://example.test", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }; let metadata = |body: &[u8]| -> Value { let response = token_response(&config, body, "room", "candidate", 2_000).unwrap(); @@ -393,6 +402,7 @@ fn token_carries_candidate_led_only_when_true() { api_secret: "secret", server_url: "wss://example.test", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }; let metadata = |body: &[u8]| -> Value { let response = token_response(&config, body, "room", "candidate", 2_000).unwrap(); @@ -428,6 +438,7 @@ fn token_carries_only_an_offered_language() { api_secret: "secret", server_url: "wss://example.test", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }; let metadata = |body: &[u8]| -> Value { let response = token_response(&config, body, "room", "candidate", 2_000).unwrap(); @@ -466,6 +477,7 @@ fn token_response_mints_the_candidate_identity() { api_secret: "devsecret", server_url: "wss://example.livekit.cloud", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }, br#"{"candidateIdentity":"candidate-a1b2c3"}"#, "interview-fixed", @@ -488,10 +500,8 @@ fn token_duration_matches_current_frontend_clamp() { (json!(1), 10), (json!(45), 45), (json!(120), 90), - (json!("bad"), 45), (json!("30"), 30), (json!(" 30 "), 30), - (json!(false), 45), (json!(true), 10), ] { let body = serde_json::to_vec(&json!({"durationMin": input})).unwrap(); @@ -501,6 +511,7 @@ fn token_duration_matches_current_frontend_clamp() { api_secret: "devsecret", server_url: "wss://example.livekit.cloud", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }, &body, "interview-fixed", @@ -518,13 +529,14 @@ fn token_duration_matches_current_frontend_clamp() { } #[test] -fn token_duration_preserves_fractional_frontend_metadata() { +fn token_duration_preserves_a_fractional_length() { let response = token_response( &TokenConfig { api_key: "devkey", api_secret: "devsecret", server_url: "wss://example.livekit.cloud", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }, br#"{"durationMin":42.8}"#, "interview-fixed", @@ -561,6 +573,7 @@ fn token_duration_stops_at_the_recording_cap() { api_secret: "devsecret", server_url: "wss://example.livekit.cloud", recording_max_min, + default_duration_min: DEFAULT_DURATION_MIN, }, &body, "interview-fixed", @@ -600,6 +613,7 @@ fn token_response_reports_the_length_it_granted() { api_secret: "devsecret", server_url: "wss://example.livekit.cloud", recording_max_min, + default_duration_min: DEFAULT_DURATION_MIN, }, &body, "interview-fixed", @@ -631,32 +645,6 @@ fn token_response_reports_the_length_it_granted() { } } -#[test] -fn token_duration_default_stops_at_the_recording_cap() { - // A caller who names no length gets the default, and the default is a - // length like any other. Handing back forty-five to a deployment that can - // only record thirty loses the same stretch, for the want of a request. - let response = token_response( - &TokenConfig { - api_key: "devkey", - api_secret: "devsecret", - server_url: "wss://example.livekit.cloud", - recording_max_min: Some(30), - }, - b"{}", - "interview-fixed", - "candidate-fixed", - 2000, - ) - .unwrap(); - let claims = claims(&response.token); - - assert_eq!( - serde_json::from_str::(claims["metadata"].as_str().unwrap()).unwrap()["durationMin"], - 30 - ); -} - #[test] fn token_duration_survives_a_recording_cap_under_the_floor() { // `f64::clamp` panics when its bounds cross, so a cap below the shortest @@ -667,6 +655,7 @@ fn token_duration_survives_a_recording_cap_under_the_floor() { api_secret: "devsecret", server_url: "wss://example.livekit.cloud", recording_max_min: Some(1), + default_duration_min: DEFAULT_DURATION_MIN, }, br#"{"durationMin":45}"#, "interview-fixed", @@ -1561,6 +1550,7 @@ fn token_resolves_voice_and_accent_independently() { api_secret: "secret", server_url: "wss://example.test", recording_max_min: None, + default_duration_min: DEFAULT_DURATION_MIN, }; for voice in ["", "Default", "Random"] .into_iter() diff --git a/tests/web/token_default.rs b/tests/web/token_default.rs new file mode 100644 index 000000000..68a366daf --- /dev/null +++ b/tests/web/token_default.rs @@ -0,0 +1,123 @@ +//! The length a token request that names none is granted: the operator's +//! `CODETRIAL_DURATION_MIN`, held to the endpoint's range and the recording +//! cap. +//! +//! Split out of `token.rs`, which is at its size ceiling. + +use super::*; + +/// A request that names no length gets the operator's `CODETRIAL_DURATION_MIN`, +/// held to the same range and recording cap as a length the lobby asked for. A +/// `durationMin` that is present but unusable names none either. +#[test] +fn token_without_a_length_gets_the_configured_default() { + let bodies: [&[u8]; 9] = [ + b"{}", + br#"{"durationMin":null}"#, + br#"{"durationMin":0}"#, + br#"{"durationMin":-5}"#, + br#"{"durationMin":false}"#, + br#"{"durationMin":"bad"}"#, + // Rust reads these as numbers; the interview page treats them as none. + br#"{"durationMin":"inf"}"#, + br#"{"durationMin":"-Infinity"}"#, + br#"{"durationMin":"NaN"}"#, + ]; + for (default_duration_min, recording_max_min, expected) in [ + (30, None, json!(30)), + (DEFAULT_DURATION_MIN, None, json!(DEFAULT_DURATION_MIN)), + // Inside the range the endpoint offers, whatever the file says. + (5, None, json!(MIN_DURATION_MIN)), + (120, None, json!(MAX_DURATION_MIN)), + // A default the cap cannot record is shortened like any other length. + (60, Some(45), json!(45)), + (30, Some(45), json!(30)), + // The built-in default on a deployment that can only record thirty + // minutes: handing back forty-five loses the same stretch. + (DEFAULT_DURATION_MIN, Some(30), json!(30)), + ] { + for body in bodies { + let response = token_response( + &TokenConfig { + api_key: "devkey", + api_secret: "devsecret", + server_url: "wss://example.livekit.cloud", + recording_max_min, + default_duration_min, + }, + body, + "interview-fixed", + "candidate-fixed", + 2000, + ) + .unwrap(); + + assert_eq!( + response.duration_min, + expected, + "default {default_duration_min} under a cap of {recording_max_min:?}, body {}", + String::from_utf8_lossy(body) + ); + // The agent is handed the length in the signed room metadata. + let metadata = claims(&response.token)["metadata"] + .as_str() + .unwrap() + .to_string(); + assert_eq!( + serde_json::from_str::(&metadata).unwrap()["durationMin"], + expected, + "metadata for default {default_duration_min} under a cap of {recording_max_min:?}" + ); + } + } +} + +/// The route hands the operator's length to the token, not the built-in one: +/// `token_response` is tested above with the field already set, so this is the +/// test that notices `/api/token` going back to the constant. +#[tokio::test] +async fn the_token_route_grants_the_configured_default_to_a_request_naming_none() { + for (configured, expected) in [(30, 30), (60, 60), (120, MAX_DURATION_MIN)] { + let mut config = web_config(); + config.guest_mode_enabled = true; + config.default_duration_min = configured; + let (base, server) = spawn_web_server_with_guest_dispatcher(config).await; + + let response = http_client() + .post(format!("{base}/api/token")) + .json(&json!({"problemId": "two-sum"})) + .send() + .await + .unwrap(); + + assert_eq!(response.status(), 200, "configured {configured}"); + let body: Value = response.json().await.unwrap(); + assert_eq!(body["durationMin"], expected, "configured {configured}"); + server.shutdown().await; + } +} + +/// `/api/session` reports the operator's length to a signed-in candidate as it +/// does to a signed-out one. A default of 60 cannot be mistaken for the +/// built-in 45, which is what a branch that forgot the field would send. +#[tokio::test] +async fn the_signed_in_session_reports_the_configured_default() { + let (mut config, cookie, path) = signed_in_web_config("signed-in-default-duration"); + config.default_duration_min = 60; + let (base, server) = spawn_web_server_with_guest_dispatcher(config).await; + + let session: Value = http_client() + .get(format!("{base}/api/session")) + .header("cookie", cookie) + .send() + .await + .unwrap() + .json() + .await + .unwrap(); + + assert_eq!(session["signedIn"], true); + assert_eq!(session["defaultDurationMin"], 60); + server.shutdown().await; + remove_database(path).await; +} diff --git a/web/app.js b/web/app.js index 3836f275c..81d9d8286 100644 --- a/web/app.js +++ b/web/app.js @@ -150,6 +150,9 @@ let deletingReports = false; /// file is still in its temporal dead zone, and reading one there throws /// before the lobby has rendered anything at all. let durationCeiling = Infinity; +/// The length a Medium or Hard problem suggests: the preselected button until +/// `/api/session` reports the operator's own `CODETRIAL_DURATION_MIN`. +let defaultDuration; /// Whether the interviewer sends candidate video, from `/api/session`. Null /// until it answers, when it fails, and when this server does not host the /// interviewer itself; the billing notice above Start stays hidden while null. @@ -725,6 +728,9 @@ nodes.attemptDownloadSelected.addEventListener("click", async () => { }); const durations = [...document.querySelectorAll("[data-duration]")]; +defaultDuration = Number( + document.querySelector("[data-duration].selected")?.dataset.duration, +); for (const button of durations) { button.addEventListener("click", () => { if (starting) return; @@ -1174,6 +1180,18 @@ async function loadAccount(generation) { throw new Error("Invalid account response"); } accountHistory = false; + // Re-suggest only when the operator's length differs from the markup's, so + // a lobby on the built-in default keeps a problem the candidate picked, or + // when a restored lobby learns a ceiling other than the one it settled + // under (a suggestion made by hand-ticking a level is not a choice). + const operatorDuration = + Number.isFinite(session.defaultDurationMin) && + session.defaultDurationMin !== defaultDuration; + const ceilingMoved = + Number.isFinite(durationCeiling) && + Number.isFinite(session.maxDurationMin) && + session.maxDurationMin !== durationCeiling; + if (operatorDuration) defaultDuration = session.defaultDurationMin; const peerLink = document.getElementById("peer-practice"); if (peerLink) peerLink.hidden = !( @@ -1182,6 +1200,19 @@ async function loadAccount(generation) { (session.identityVerified ?? session.user?.identityVerified) ); applyDurationCeiling(session.maxDurationMin); + // Not under a start already on its way: it took the length on screen, and + // moving the row now would show one interview and run another. + if ((operatorDuration || ceilingMoved) && !starting) { + // A problem picked by hand before the session answered suggested its own + // length from its own difficulty; the checkboxes would overrule it. + setDuration( + suggestedDuration( + manualProblem && problem + ? new Set([problem.difficulty]) + : selectedDifficulties(), + ), + ); + } candidateVideo = typeof session.candidateVideo === "boolean" ? session.candidateVideo @@ -1809,13 +1840,16 @@ function mark(button, on) { button.setAttribute("aria-pressed", String(on)); } -/// Capped at the server's own default length, which src/config.rs holds at or -/// under the recording cap, so nothing the lobby picks on its own outlives its -/// recording. Only the suggestion is capped: the sixty minute button is one -/// click away and a deployment can set the two environment variables apart, so -/// this narrows the default rather than guaranteeing anything. +/// The operator's default length for Medium and Hard, and no more than thirty +/// for Easy. Snapped to a button the row offers, so a default of 20 or 75 still +/// leaves one selected; the ceiling is applied afterwards by `setDuration`. function suggestedDuration(difficulties) { - return difficulties.has("Medium") || difficulties.has("Hard") ? 45 : 30; + const hard = difficulties.has("Medium") || difficulties.has("Hard"); + const wanted = hard ? defaultDuration : Math.min(30, defaultDuration); + const offered = durations.map((button) => Number(button.dataset.duration)); + return offered.reduce((best, value) => + Math.abs(value - wanted) < Math.abs(best - wanted) ? value : best, + ); } function setInterviewLoop(value) { diff --git a/web/index.html b/web/index.html index 85003c502..8c4a6559e 100644 --- a/web/index.html +++ b/web/index.html @@ -103,9 +103,9 @@

Choose a problem

difficulties, if one is available, after another preflight.

-
Difficulty diff --git a/web/interview.js b/web/interview.js index 1b4a8f6f8..b2ddb82ab 100644 --- a/web/interview.js +++ b/web/interview.js @@ -367,15 +367,18 @@ let languages = []; // A delayed judge may make the requested C tab available after the first // paint. Do not switch to it after the candidate has chosen a tab or edited. let languageChoiceCommitted = false; +/// Whole minutes, as the agent reads them from the room metadata. +const named = Math.trunc(Number(params.get("duration"))); +/// Whether the URL named a usable length, which `?duration=`, `?duration=abc`, +/// `?duration=30abc`, `?duration=Infinity`, `0` and `-5` do not: decided once, +/// here, for the token request and for the round plan alike. +const namedDuration = Number.isFinite(named) && named > 0; /// What the lobby asked for, until `/api/token` says what it got. The range -/// here mirrors the server's own and is the fallback for a URL that arrives -/// without passing through the lobby; the answer below is what the interview -/// actually runs on. -let durationMin = clamp( - Number.parseInt(params.get("duration") || "45", 10) || 45, - 10, - 90, -); +/// here mirrors the server's own, and 45 is only the length shown until the +/// answer lands for a URL that arrives without passing through the lobby: such +/// a request names no length, so the server's configured default decides. The +/// answer below is what the interview actually runs on. +let durationMin = namedDuration ? clamp(named, 10, 90) : 45; const interviewLoop = codingLoop(params.get("loop")); /// Whether this interview is held at a whiteboard rather than in the editor. /// @@ -470,8 +473,8 @@ let codingMinutes = durationMin - behavioralMinutes; /// round split and the saved report describing an interview nobody is having. /// /// An answer that is missing or unusable leaves the request in place: the page -/// still runs, on the length the URL named, exactly as it did before the -/// server sent this. +/// still runs, on the length shown so far (the URL's, else the provisional 45), +/// exactly as it did before the server sent this. function applyGrantedDuration(granted) { if (typeof granted !== "number" || !Number.isFinite(granted) || granted <= 0) return; @@ -480,7 +483,7 @@ function applyGrantedDuration(granted) { interviewLoop === "coding_behavioral" ? Math.min(8, durationMin) : 0; codingMinutes = durationMin - behavioralMinutes; // The budget is on screen by now: `bindEvents` wrote it during setup, from - // the length the URL asked for. Leaving it there would put the old number in + // the length shown so far. Leaving it there would put the old number in // front of the candidate for the whole interview. renderRoundPlan(); } @@ -1221,7 +1224,14 @@ function bindEvents() { nodes.jimMute.addEventListener("click", () => jimVolume.toggleMute()); nodes.jimVolumeReset.addEventListener("click", () => jimVolume.reset()); renderJimVolume(); - renderRoundPlan(); + // A URL that names no length gets the server's default, so the plan waits for + // `/api/session` rather than show 45 minutes and then jump to the real one. + if (namedDuration) renderRoundPlan(); + else + void sessionPromise.then((session) => { + applyGrantedDuration(session?.defaultDurationMin); + renderRoundPlan(); + }); nodes.problemTab.addEventListener("click", () => selectTab("problem")); nodes.transcriptTab.addEventListener("click", () => selectTab("transcript")); nodes.mic.addEventListener("click", toggleMicrophone); @@ -2042,7 +2052,7 @@ async function connect(preflight, presenting = false) { ...(previous ? { previousAssignment: previous } : {}), } : { problemId: problem.page }), - durationMin, + ...(namedDuration ? { durationMin } : {}), interviewId, interviewLoop, interviewMode: mode,