Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
417 changes: 258 additions & 159 deletions desktop/src-tauri/src/commands/channels.rs

Large diffs are not rendered by default.

126 changes: 126 additions & 0 deletions desktop/src-tauri/src/commands/channels_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// channels.rs under the per-file line cap.

use super::*;
use crate::models::ChannelInfo;
use nostr::{EventBuilder, Keys, Kind, Tag, Timestamp};

/// Build a signed event for testing with the given kind, content, and tags.
Expand Down Expand Up @@ -266,6 +267,131 @@ fn duplicate_channel_rejection_is_ensure_success_only() {
));
}

// ── compute_channels_hash ─────────────────────────────────────────────────────

fn make_channel(id: &str, name: &str, last_message_at: Option<String>) -> ChannelInfo {
ChannelInfo {
id: id.to_string(),
name: name.to_string(),
channel_type: "stream".to_string(),
visibility: "open".to_string(),
description: "".to_string(),
topic: None,
purpose: None,
member_count: 0,
member_pubkeys: Vec::new(),
last_message_at,
archived_at: None,
participants: Vec::new(),
participant_pubkeys: Vec::new(),
is_member: true,
ttl_seconds: None,
ttl_deadline: None,
}
}

#[test]
fn hash_is_order_insensitive() {
let c1 = make_channel("aaa", "Alpha", None);
let c2 = make_channel("bbb", "Beta", None);
let c3 = make_channel("aaa", "Alpha", None);
let c4 = make_channel("bbb", "Beta", None);

assert_eq!(
compute_channels_hash(&[c1, c2]),
compute_channels_hash(&[c4, c3]),
"hash must be insensitive to channel list ordering",
);
}

#[test]
fn hash_ignores_last_message_at() {
let c_none = make_channel("chan-1", "Alpha", None);
let c_some = make_channel("chan-1", "Alpha", Some("2026-01-01T00:00:00Z".to_string()));

assert_eq!(
compute_channels_hash(&[c_none]),
compute_channels_hash(&[c_some]),
"hash must be insensitive to last_message_at",
);
}

#[test]
fn hash_changes_on_metadata_change() {
let c1 = make_channel("chan-1", "Alpha", None);
let c2 = make_channel("chan-1", "AlphaRenamed", None);

assert_ne!(
compute_channels_hash(&[c1]),
compute_channels_hash(&[c2]),
"hash must change when channel name changes",
);
}

#[test]
fn hash_changes_on_membership_change() {
let mut c1 = make_channel("chan-1", "Alpha", None);
let mut c2 = make_channel("chan-1", "Alpha", None);
c1.member_pubkeys = vec![PK_A.to_string()];
c2.member_pubkeys = vec![PK_A.to_string(), PK_B.to_string()];

assert_ne!(
compute_channels_hash(&[c1]),
compute_channels_hash(&[c2]),
"hash must change when member_pubkeys changes",
);
}

#[test]
fn not_modified_returns_none_when_hash_matches() {
let channels = vec![make_channel("chan-1", "General", None)];
let hash = compute_channels_hash(&channels);

// Mirror the get_channels command decision logic.
let known_hash = Some(hash.clone());
let is_not_modified = known_hash.as_deref() == Some(hash.as_str());

assert!(
is_not_modified,
"identical hash must trigger the not-modified short-circuit",
);
}

#[test]
fn not_modified_does_not_trigger_on_hash_mismatch() {
let channels = vec![make_channel("chan-1", "General", None)];
let hash = compute_channels_hash(&channels);
let known_hash = Some("0000000000000000".to_string());

let is_not_modified = known_hash.as_deref() == Some(hash.as_str());

assert!(
!is_not_modified,
"stale hash must NOT trigger the not-modified short-circuit",
);
}

#[test]
fn hash_is_stable_for_same_input() {
// Verifies that the FNV-1a output is deterministic across calls within
// the same process (unlike std DefaultHasher which uses random seeds).
let channels = vec![
make_channel("aaa", "General", Some("2026-01-01T00:00:00Z".to_string())),
make_channel("bbb", "Random", None),
];
let first = compute_channels_hash(&channels);
let channels2 = vec![
make_channel("aaa", "General", None), // last_message_at change is ignored
make_channel("bbb", "Random", None),
];
let second = compute_channels_hash(&channels2);

assert_eq!(
first, second,
"hash must be deterministic and ignore last_message_at"
);
}

#[test]
fn starter_match_requires_open_unarchived_stream_by_normalized_name() {
let spec = &STARTER_CHANNELS[0];
Expand Down
19 changes: 19 additions & 0 deletions desktop/src-tauri/src/commands/window_vibrancy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ pub fn set_window_vibrancy(
.ok_or_else(|| "main window not found".to_string())?;

if !enabled {
// Restore opaque backing first: a failure of clear_vibrancy leaves
// the window with an opaque canvas and a blur layer hidden behind
// it, not a see-through webview. Either mixed state self-corrects
// on the next toggle. `None` resets both the NSWindow backing and
// the WKWebView canvas to their platform defaults.
window
.set_background_color(None)
Comment thread
wpfleger96 marked this conversation as resolved.
Resolved
.map_err(|e| e.to_string())?;
clear_vibrancy(&window).map_err(|e| e.to_string())?;
return Ok(());
}
Expand All @@ -58,7 +66,18 @@ pub fn set_window_vibrancy(
// clear is a no-op (returns `false`) when none is present.
let _ = clear_vibrancy(&window);

// Install the blur layer first: a failure of set_background_color leaves
// the window opaque with vibrancy behind it, not a see-through webview.
// Either mixed state self-corrects on the next toggle.
apply_vibrancy(&window, material, None, None).map_err(|e| e.to_string())?;

// Make the WKWebView canvas and NSWindow backing transparent so native
// vibrancy shows through. Must follow apply_vibrancy so the blur layer
// is present before the canvas becomes see-through.
window
.set_background_color(Some(tauri::window::Color(0, 0, 0, 0)))
Comment thread
wpfleger96 marked this conversation as resolved.
Resolved
.map_err(|e| e.to_string())?;

Ok(())
}

Expand Down
5 changes: 3 additions & 2 deletions desktop/src-tauri/src/initial_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ pub(crate) fn reveal_initial_window<R: tauri::Runtime>(window: &tauri::Window<R>

#[cfg(target_os = "macos")]
pub(crate) fn set_initial_window_backing<R: tauri::Runtime>(window: &tauri::Window<R>) {
// The window remains transparent at runtime for vibrancy. Use an opaque
// native backing only across the first visible frames so the previous app
// Runtime transparency exists only while glass is enabled via
// `set_window_vibrancy`; with glass off the webview stays opaque. Use an
// opaque native backing across the first visible frames so the previous app
// cannot show through before WebKit has submitted its first surface.
if let Err(error) = window.set_background_color(Some(tauri::window::Color(17, 21, 24, 255))) {
eprintln!("buzz-desktop: failed to set initial window backing: {error}");
Expand Down
7 changes: 0 additions & 7 deletions desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,6 @@ pub fn run() {
// on macOS/Windows.
linux_media::enable_media_capture(&webview);

#[cfg(target_os = "macos")]
if let Err(error) = webview
.set_background_color(Some(tauri::window::Color(0, 0, 0, 0)))
{
eprintln!("buzz-desktop: failed to make the macOS webview transparent: {error}");
}

// macOS applies the restored geometry asynchronously. Wait
// for several identical outer bounds and for React to
// commit the startup surface before revealing it.
Expand Down
15 changes: 15 additions & 0 deletions desktop/src-tauri/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,21 @@ fn default_true() -> bool {
true
}

/// Response payload for `get_channels`. When the caller supplies a hash that
/// matches the computed stable hash, `channels` is `None` so the multi-MB
/// channel list is not serialized across IPC. `last_messages` is always
/// included — it is cheap and changes frequently (every new message).
#[derive(Serialize)]
pub struct GetChannelsPayload {
pub hash: String,
/// `None` on a not-modified response (hash matched); `Some` with the full
/// sorted list otherwise.
pub channels: Option<Vec<ChannelInfo>>,
/// Map of channel id → ISO-8601 timestamp of its most recent message.
/// Empty for channels with no messages.
pub last_messages: std::collections::HashMap<String, String>,
}

// ── Social / Contact list ───────────────────────────────────────────────────

#[derive(Serialize, Deserialize)]
Expand Down
89 changes: 89 additions & 0 deletions desktop/src/features/agents/focusRefetchPolicy.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import assert from "node:assert/strict";
import { afterEach, test } from "node:test";

import {
focusManager,
QueryClient,
QueryObserver,
} from "@tanstack/react-query";

import {
AGENTS_FOCUS_STALE_TIME_MS,
MANAGED_AGENT_LOG_FOCUS_STALE_TIME_MS,
} from "./hooks.ts";

afterEach(() => {
focusManager.setFocused(undefined);
});

async function focusRefetchCount({ ageMs, staleTime }) {
focusManager.setFocused(false);
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
queryClient.mount();

const queryKey = ["focus-refetch-policy", staleTime, ageMs];
queryClient.setQueryData(queryKey, "cached", {
updatedAt: Date.now() - ageMs,
});
let fetchCount = 0;
const observer = new QueryObserver(queryClient, {
queryKey,
queryFn: async () => {
fetchCount += 1;
return "refetched";
},
refetchOnMount: false,
refetchOnWindowFocus: true,
staleTime,
});
const unsubscribe = observer.subscribe(() => {});

focusManager.setFocused(true);
await new Promise((resolve) => setImmediate(resolve));

unsubscribe();
queryClient.unmount();
return fetchCount;
}

test("agents: skips fresh focus refetch", async () => {
assert.equal(
await focusRefetchCount({
ageMs: AGENTS_FOCUS_STALE_TIME_MS - 1_000,
staleTime: AGENTS_FOCUS_STALE_TIME_MS,
}),
0,
);
});

test("agents: refetches genuinely stale data on focus", async () => {
assert.equal(
await focusRefetchCount({
ageMs: AGENTS_FOCUS_STALE_TIME_MS + 1,
staleTime: AGENTS_FOCUS_STALE_TIME_MS,
}),
1,
);
});

test("managed-agent-log: skips focus refetch when data is fresher than one poll tick", async () => {
assert.equal(
await focusRefetchCount({
ageMs: MANAGED_AGENT_LOG_FOCUS_STALE_TIME_MS - 1_000,
staleTime: MANAGED_AGENT_LOG_FOCUS_STALE_TIME_MS,
}),
0,
);
});

test("managed-agent-log: refetches on focus when data is older than one poll tick", async () => {
assert.equal(
await focusRefetchCount({
ageMs: MANAGED_AGENT_LOG_FOCUS_STALE_TIME_MS + 1,
staleTime: MANAGED_AGENT_LOG_FOCUS_STALE_TIME_MS,
}),
1,
);
});
Loading
Loading