Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion Sources/Ontology/Types/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ public struct Event: Hashable, Sendable {
/// The name/title of the event
public var name: String?

/// Description of the event
public var description: String?

/// The calendar this event belongs to
public var calendar: String?

Expand Down Expand Up @@ -47,6 +50,7 @@ public struct Event: Hashable, Sendable {
/// Initialize an Event with an EventKit event
public init(_ event: EKEvent) {
self.name = event.title
self.description = event.notes
self.calendar = event.calendar?.title
self.startDate = DateTime(event.startDate, timeZone: event.timeZone)
self.endDate = DateTime(event.endDate, timeZone: event.timeZone)
Expand All @@ -58,7 +62,7 @@ public struct Event: Hashable, Sendable {

extension Event: Codable {
private enum CodingKeys: String, CodingKey {
case name, startDate, endDate, location, url, calendar
case name, description, startDate, endDate, location, url, calendar
}

public func encode(to encoder: Encoder) throws {
Expand All @@ -77,6 +81,7 @@ extension Event: Codable {

// Encode properties
try container.encodeIfPresent(name, forKey: .attribute(.name))
try container.encodeIfPresent(description, forKey: .attribute(.description))
try container.encodeIfPresent(calendar, forKey: .attribute(.calendar))
try container.encodeIfPresent(startDate, forKey: .attribute(.startDate))
try container.encodeIfPresent(endDate, forKey: .attribute(.endDate))
Expand All @@ -103,6 +108,7 @@ extension Event: Codable {

// Decode properties
name = try container.decodeIfPresent(String.self, forKey: .attribute(.name))
description = try container.decodeIfPresent(String.self, forKey: .attribute(.description))
calendar = try container.decodeIfPresent(String.self, forKey: .attribute(.calendar))
startDate = try container.decodeIfPresent(DateTime.self, forKey: .attribute(.startDate))
endDate = try container.decodeIfPresent(DateTime.self, forKey: .attribute(.endDate))
Expand Down
42 changes: 42 additions & 0 deletions Tests/OntologyTests/EventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,48 @@ struct EventTests {
#expect(minimalEvent.url == nil)
}

@Test("Event initialization preserves notes as description")
func testNotesPreservation() throws {
let eventStore = EKEventStore()
let event = EKEvent(eventStore: eventStore)

event.title = "Test Event"
event.startDate = Date(timeIntervalSinceReferenceDate: 0)
event.endDate = Date(timeIntervalSinceReferenceDate: 3600)
event.notes = "Bring the signed copy"

let ontologyEvent = Event(event)

#expect(ontologyEvent.description == "Bring the signed copy")

let withoutNotes = EKEvent(eventStore: eventStore)
withoutNotes.title = "Test Event"
withoutNotes.startDate = Date(timeIntervalSinceReferenceDate: 0)
withoutNotes.endDate = Date(timeIntervalSinceReferenceDate: 3600)

#expect(Event(withoutNotes).description == nil)
}

@Test("Event round-trip serialization preserves notes")
func testNotesRoundTrip() throws {
let eventStore = EKEventStore()
let event = EKEvent(eventStore: eventStore)

event.title = "Test Event"
event.startDate = Date(timeIntervalSinceReferenceDate: 0)
event.endDate = Date(timeIntervalSinceReferenceDate: 3600)
event.notes = "Bring the signed copy"

let original = Event(event)
let encoded = try JSONEncoder().encode(original)

let json = try JSONSerialization.jsonObject(with: encoded) as! [String: Any]
#expect(json["description"] as? String == "Bring the signed copy")

let decoded = try JSONDecoder().decode(Event.self, from: encoded)
#expect(decoded.description == original.description)
}

@Test("Event JSON-LD encoding preserves all properties")
func testJSONLDEncoding() throws {
let eventStore = EKEventStore()
Expand Down