Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[ensu] Simplify notes implementation and tests

setalp6ebf10aa02e2
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatMessages.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatMessages.kt
index 260e0c70be..85d9b38411 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatMessages.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatMessages.kt
@@ -7,7 +7,6 @@ import androidx.compose.animation.scaleIn
import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
-import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.gestures.scrollBy
import androidx.compose.foundation.isSystemInDarkTheme
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.kt
index a30ea2983e..1ffa613c3f 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.kt
@@ -94,10 +94,11 @@ internal class AndroidNotesSource(
private fun queryChildren(id: String): Map<String, Entry> {
val entries = query(DocumentsContract.buildChildDocumentsUriUsingTree(tree, id))
- if (entries.map { it.name }.toSet().size != entries.size) {
+ val children = entries.associateBy { it.name }
+ if (children.size != entries.size) {
throw NotesException.SourceRead("The folder contains duplicate document names")
}
- return entries.associateBy { it.name }
+ return children
}
fun sameRoot(other: AndroidNotesSource): Boolean =
diff --git a/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesMaintenanceGateTest.kt b/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesMaintenanceGateTest.kt
index 6665589325..c623c47791 100644
--- a/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesMaintenanceGateTest.kt
+++ b/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesMaintenanceGateTest.kt
@@ -1,8 +1,5 @@
package io.ente.ensu.notes
-import java.util.concurrent.CyclicBarrier
-import java.util.concurrent.Executors
-import java.util.concurrent.TimeUnit
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertSame
@@ -11,32 +8,28 @@ import org.junit.Test
class NotesMaintenanceGateTest {
@Test
- fun concurrentSuspensionEitherFindsOrPreventsTheRun() {
- val executor = Executors.newFixedThreadPool(2)
- try {
- val gate = NotesMaintenanceGate<Any>()
- val run = Any()
- val start = CyclicBarrier(2)
- val admission = executor.submit<Boolean> {
- start.await(5, TimeUnit.SECONDS)
- gate.admit(run)
- }
- val suspension = executor.submit<Any?> {
- start.await(5, TimeUnit.SECONDS)
- gate.suspend()
- }
+ fun suspensionPreventsAdmissionUntilResumed() {
+ val gate = NotesMaintenanceGate<Any>()
+ val run = Any()
- if (admission.get(5, TimeUnit.SECONDS)) {
- assertSame(run, suspension.get(5, TimeUnit.SECONDS))
- } else {
- assertNull(suspension.get(5, TimeUnit.SECONDS))
- }
- gate.finish(run)
- assertFalse(gate.available)
- gate.resume()
- assertTrue(gate.available)
- } finally {
- executor.shutdownNow()
- }
+ assertNull(gate.suspend())
+ assertFalse(gate.admit(run))
+ gate.resume()
+
+ assertTrue(gate.admit(run))
+ }
+
+ @Test
+ fun suspensionReturnsTheActiveRun() {
+ val gate = NotesMaintenanceGate<Any>()
+ val run = Any()
+ assertTrue(gate.admit(run))
+
+ assertSame(run, gate.suspend())
+ gate.finish(run)
+ assertFalse(gate.available)
+ gate.resume()
+
+ assertTrue(gate.available)
}
}
diff --git a/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesReadTest.kt b/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesReadTest.kt
index 66c2818bb2..49c018b510 100644
--- a/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesReadTest.kt
+++ b/android/apps/ensu/app/src/test/java/io/ente/ensu/notes/NotesReadTest.kt
@@ -8,8 +8,6 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
-import kotlinx.coroutines.sync.Mutex
-import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
@@ -23,17 +21,14 @@ class NotesReadTest {
val cancelled = CountDownLatch(1)
val drain = CountDownLatch(1)
val released = AtomicBoolean()
- val gate = Mutex()
launch(owner) {
- gate.withLock {
- try {
- runNotesRead(cancelled::countDown) {
- entered.countDown()
- check(drain.await(5, TimeUnit.SECONDS))
- }
- } finally {
- released.set(true)
+ try {
+ runNotesRead(cancelled::countDown) {
+ entered.countDown()
+ check(drain.await(5, TimeUnit.SECONDS))
}
+ } finally {
+ released.set(true)
}
}
@@ -42,13 +37,11 @@ class NotesReadTest {
owner.cancel()
assertTrue(withContext(Dispatchers.IO) { cancelled.await(5, TimeUnit.SECONDS) })
assertFalse(released.get())
- assertTrue(gate.isLocked)
} finally {
drain.countDown()
owner.cancelAndJoin()
}
assertTrue(released.get())
- assertFalse(gate.isLocked)
}
}
diff --git a/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift b/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift
index e04d8cf5d9..a39f367ad0 100644
--- a/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift
+++ b/apple/apps/ensu/Ensu/Chat/MessageList/MessageListComponents.swift
@@ -313,7 +313,18 @@ struct AssistantMessageBubbleView: View {
private func sourceChips(_ labels: [String]) -> some View {
ForEach(Array(labels.enumerated()), id: \.offset) { _, label in
- SourceChip(label: label) { showSources = true }
+ Button { showSources = true } label: {
+ Text(label)
+ .font(EnsuTypography.small)
+ .foregroundStyle(EnsuColor.textPrimary)
+ .padding(.horizontal, EnsuSpacing.md)
+ .padding(.vertical, EnsuSpacing.sm)
+ .frame(minHeight: 44)
+ .background(EnsuColor.fillFaint)
+ .clipShape(RoundedRectangle(cornerRadius: EnsuCornerRadius.button))
+ }
+ .buttonStyle(.plain)
+ .accessibilityHint("View sources used in this response")
}
}
}
diff --git a/apple/apps/ensu/Ensu/Components/EnsuComponents.swift b/apple/apps/ensu/Ensu/Components/EnsuComponents.swift
index 8fe0921543..8d3e90f7e2 100644
--- a/apple/apps/ensu/Ensu/Components/EnsuComponents.swift
+++ b/apple/apps/ensu/Ensu/Components/EnsuComponents.swift
@@ -14,26 +14,6 @@ struct EnsuCard<Content: View>: View {
}
}
-struct SourceChip: View {
- let label: String
- let action: () -> Void
-
- var body: some View {
- Button(action: action) {
- Text(label)
- .font(EnsuTypography.small)
- .foregroundStyle(EnsuColor.textPrimary)
- .padding(.horizontal, EnsuSpacing.md)
- .padding(.vertical, EnsuSpacing.sm)
- .frame(minHeight: 44)
- .background(EnsuColor.fillFaint)
- .clipShape(RoundedRectangle(cornerRadius: EnsuCornerRadius.button))
- }
- .buttonStyle(.plain)
- .accessibilityHint("View sources used in this response")
- }
-}
-
struct AttributionSheet<Content: View>: View {
let title: String
@ViewBuilder var content: () -> Content
diff --git a/apple/apps/ensu/Ensu/Services/IOSNotesSource.swift b/apple/apps/ensu/Ensu/Services/IOSNotesSource.swift
index 78c8eb3b7c..66efc17550 100644
--- a/apple/apps/ensu/Ensu/Services/IOSNotesSource.swift
+++ b/apple/apps/ensu/Ensu/Services/IOSNotesSource.swift
@@ -183,7 +183,7 @@ final class IOSNotesSource: NotesSource, @unchecked Sendable {
return NSError(domain: NSPOSIXErrorDomain, code: Int(code))
}
- static func documentReadError(_ error: Error, root: URL) -> Error {
+ private static func documentReadError(_ error: Error, root: URL) -> Error {
guard isMissingFile(error) else { return accessError(error) }
let fd = Darwin.open(root.path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW)
guard fd >= 0 else {
diff --git a/apple/apps/ensu/Ensu/Services/NotesProvider.swift b/apple/apps/ensu/Ensu/Services/NotesProvider.swift
index 6db6731ed0..9bdce9b591 100644
--- a/apple/apps/ensu/Ensu/Services/NotesProvider.swift
+++ b/apple/apps/ensu/Ensu/Services/NotesProvider.swift
@@ -224,10 +224,8 @@ actor NotesProvider {
) {
return url.standardizedFileURL.resolvingSymlinksInPath().path
}
- if let path = record.canonicalPath {
- return path
- }
- return NSURL.resourceValues(forKeys: [.pathKey], fromBookmarkData: record.bookmark)?[.pathKey] as? String
+ return record.canonicalPath ??
+ (NSURL.resourceValues(forKeys: [.pathKey], fromBookmarkData: record.bookmark)?[.pathKey] as? String)
}
private static func handle(_ root: URL, _ id: String) throws -> NotesCollection {
diff --git a/apple/apps/ensu/Ensu/Services/NotesStore.swift b/apple/apps/ensu/Ensu/Services/NotesStore.swift
index 01a48ef3cf..4b7c99b75d 100644
--- a/apple/apps/ensu/Ensu/Services/NotesStore.swift
+++ b/apple/apps/ensu/Ensu/Services/NotesStore.swift
@@ -1,7 +1,7 @@
import Foundation
import SwiftUI
-enum NotesStatus: String {
+enum NotesStatus {
case pending, indexing, updating, ready, unavailable, error
}
@@ -412,12 +412,13 @@ final class NotesStore: ObservableObject, ModelMaintenance {
}
private static func state(_ record: NotesCollectionInfo, _ summary: NotesSummary, progress: UInt8? = nil) -> NoteCollectionState {
let ready = summary.initialComplete && summary.documentCount > 0
+ let empty = summary.initialComplete && !ready
return NoteCollectionState(id: record.id, label: record.label,
status: ready ? .ready : summary.initialComplete ? .error : .pending,
documentCount: summary.documentCount, lastUpdatedAtMs: summary.lastUpdatedAtMs, indexAvailable: ready,
progress: summary.initialComplete ? nil : progress,
- error: summary.initialComplete && !ready ? emptyNotes : nil,
- completedEmpty: summary.initialComplete && !ready)
+ error: empty ? emptyNotes : nil,
+ completedEmpty: empty)
}
}
diff --git a/apple/apps/ensu/EnsuTests/NotesIntegrationTests.swift b/apple/apps/ensu/EnsuTests/NotesIntegrationTests.swift
index a70a75c3a1..96f39adfc0 100644
--- a/apple/apps/ensu/EnsuTests/NotesIntegrationTests.swift
+++ b/apple/apps/ensu/EnsuTests/NotesIntegrationTests.swift
@@ -11,31 +11,10 @@ private final class UnavailableNotesSource: NotesSource {
}
final class NotesIntegrationTests: XCTestCase {
- private let collectionID = "123e4567-e89b-12d3-a456-426614174000"
-
- func testMissingDocumentRequiresReconciliationOnlyWhileRootIsAvailable() throws {
- let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
- try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
- defer { try? FileManager.default.removeItem(at: root) }
- XCTAssertThrowsError(try Data(contentsOf: root.appendingPathComponent("missing.md"))) {
- XCTAssertEqual(IOSNotesSource.documentReadError($0, root: root) as? NotesError, .SourceChanged)
- }
- let missing = NSError(domain: NSPOSIXErrorDomain, code: Int(ENOENT))
- let coordinated = NSError(domain: NSCocoaErrorDomain, code: NSFileReadUnknownError,
- userInfo: [NSUnderlyingErrorKey: missing])
- XCTAssertEqual(IOSNotesSource.documentReadError(coordinated, root: root) as? NotesError, .SourceChanged)
- let transient = NSError(domain: NSCocoaErrorDomain, code: NSFileReadUnknownError)
- guard case .SourceRead = IOSNotesSource.documentReadError(transient, root: root) as? NotesError else {
- return XCTFail("A provider failure must remain a read error")
- }
- try FileManager.default.removeItem(at: root)
- XCTAssertEqual(IOSNotesSource.documentReadError(coordinated, root: root) as? NotesError, .Unavailable)
- }
-
func testNativeSourceFailureAndCancellationCrossTheBridge() throws {
let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
defer { try? FileManager.default.removeItem(at: root) }
- let collection = try NotesCollection(indexRoot: root.path, collectionId: collectionID)
+ let collection = try NotesCollection(indexRoot: root.path, collectionId: UUID().uuidString.lowercased())
let source = UnavailableNotesSource()
XCTAssertThrowsError(try collection.inspectFreshness(source: source, cancellation: NotesCancellation())) {
XCTAssertEqual($0 as? NotesError, .Unavailable)
diff --git a/rust/crates/ensu/src/notes/indexing.rs b/rust/crates/ensu/src/notes/indexing.rs
index 8e9b049027..e0b816e205 100644
--- a/rust/crates/ensu/src/notes/indexing.rs
+++ b/rust/crates/ensu/src/notes/indexing.rs
@@ -367,7 +367,7 @@ mod tests {
document_id: document_id.to_string(),
..source()
};
- let inventory = (0..12)
+ let inventory = (0..2)
.map(|index| source_for(&format!("note-{index:02}.md")))
.collect::<Vec<_>>();
let forced_document_ids = inventory
@@ -428,7 +428,6 @@ mod tests {
assert!(!writer.initial_inventory_complete());
assert_eq!(writer.indexed_document_ids().collect::<Vec<_>>(), embedded);
}
- let resumed_progress = RefCell::new(Vec::new());
let outcome = index_notes_collection(
input,
|| Ok(()),
@@ -438,12 +437,11 @@ mod tests {
Ok(Some(embedding(document)))
},
verify_revision,
- |progress| resumed_progress.borrow_mut().push(progress),
+ |_| {},
)
.unwrap();
assert!(outcome.ready());
- assert_eq!(resumed_progress.borrow()[0].processed_document_count, 0);
let document_ids = inventory
.iter()
.map(|source| source.document_id.as_str())
diff --git a/rust/crates/ensu/src/notes/maintenance.rs b/rust/crates/ensu/src/notes/maintenance.rs
index cfc9a791c6..fa170d33df 100644
--- a/rust/crates/ensu/src/notes/maintenance.rs
+++ b/rust/crates/ensu/src/notes/maintenance.rs
@@ -135,7 +135,7 @@ mod tests {
}
#[test]
- fn freshness_preserves_source_failures_and_cancellation() {
+ fn freshness_detects_changes_during_content_reads() {
let root = tempfile::tempdir().unwrap();
let writer = fixture(root.path(), Some(b"note"), true);
let inventory = [source(b"note", None)];
@@ -148,20 +148,6 @@ mod tests {
.unwrap();
assert!(changed.changed);
assert_eq!(changed.forced_document_ids, ["note.md"]);
- assert!(matches!(
- inspect_notes_freshness(&writer, &inventory, || Ok(()), |_| Err("offline")),
- Err(NotesIndexingError::Adapter("offline"))
- ));
- assert!(matches!(
- inspect_notes_freshness(
- &writer,
- &inventory,
- || Err("cancelled"),
- |_| { panic!("cancelled scans must not read content") }
- ),
- Err(NotesIndexingError::Adapter("cancelled"))
- ));
- assert_eq!(writer.indexed_document_count(), 1);
}
#[test]
diff --git a/rust/crates/ensu/src/notes/writer.rs b/rust/crates/ensu/src/notes/writer.rs
index 6a0609bcd7..aed842be9c 100644
--- a/rust/crates/ensu/src/notes/writer.rs
+++ b/rust/crates/ensu/src/notes/writer.rs
@@ -997,57 +997,11 @@ mod tests {
Err(NotesError::CollectionTooLarge(_))
));
writer.source_bytes = 0;
- let entry =
- manifest_document(&document, &source, &document.revision, &document.revision).unwrap();
- for index in 0..NOTES_MAX_COLLECTION_DOCUMENTS {
- writer
- .manifest
- .documents
- .insert(format!("note-{index:05}.md"), entry.clone());
- }
+ writer.manifest_bytes = 8 * 1024 * 1024;
assert!(matches!(
writer.validate_document(&document, &source),
Err(NotesError::CollectionTooLarge(_))
));
- (writer.source_bytes, writer.chunk_count) = manifest_capacity(&writer.manifest).unwrap();
- writer.manifest_bytes = serialize_manifest_for_publish(&writer.manifest, &collection())
- .unwrap()
- .len() as u64;
- let replacement = prepared("note-00000.md", "new note");
- writer
- .validate_document(&replacement, &metadata(&replacement, 8))
- .unwrap();
-
- writer.manifest.documents.clear();
- let long_id = |index| format!("{}-{index:04}.md", "a".repeat(4_000));
- writer.manifest.documents.insert(long_id(0), entry.clone());
- let first_size = serialize_manifest_for_publish(&writer.manifest, &collection())
- .unwrap()
- .len();
- writer.manifest.documents.insert(long_id(1), entry.clone());
- let entry_size = serialize_manifest_for_publish(&writer.manifest, &collection())
- .unwrap()
- .len()
- - first_size;
- let fitting_count = 1 + (8 * 1024 * 1024 - first_size) / entry_size;
- for index in 2..fitting_count {
- writer
- .manifest
- .documents
- .insert(long_id(index), entry.clone());
- }
- (writer.source_bytes, writer.chunk_count) = manifest_capacity(&writer.manifest).unwrap();
- writer.manifest_bytes = serialize_manifest_for_publish(&writer.manifest, &collection())
- .unwrap()
- .len() as u64;
- let document = PreparedNotesDocument {
- document_id: long_id(fitting_count),
- ..document
- };
- assert!(matches!(
- writer.validate_document(&document, &metadata(&document, 4)),
- Err(NotesError::CollectionTooLarge(_))
- ));
}
#[test]