Skip to content

Commit 42b0f77

Browse files
committed
Update the tests
1 parent 7b4abe2 commit 42b0f77

File tree

12 files changed

+125
-263
lines changed

12 files changed

+125
-263
lines changed

Modules/Sources/WordPressIntelligence/IntelligenceService.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public actor IntelligenceService {
4848

4949
/// Summarizes a WordPress post.
5050
@available(iOS 26, *)
51-
public func summarizePost(content: String) -> LanguageModelSession.ResponseStream<String> {
52-
PostSummary.execute(content: content)
51+
public func summarizePost(content: String) async throws -> String {
52+
try await PostSummary.execute(content: content)
5353
}
5454

5555
/// Summarizes a support ticket to a short title.
@@ -67,7 +67,7 @@ public actor IntelligenceService {
6767

6868
/// Extracts relevant text from post content, removing HTML and limiting size.
6969
public nonisolated static func extractRelevantText(from post: String, ratio: CGFloat = 0.6) -> String {
70-
let extract = try? IntelligenceUtilities.extractRelevantText(from: post)
70+
let extract = try? ContentExtractor.extractRelevantText(from: post)
7171
let postSizeLimit = Double(IntelligenceService.contextSizeLimit) * ratio
7272
return String((extract ?? post).prefix(Int(postSizeLimit)))
7373
}

Modules/Sources/WordPressIntelligence/UseCases/ExcerptGeneration.swift

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
import Foundation
22
import FoundationModels
33

4-
/// Excerpt generation for WordPress posts.
5-
///
6-
/// Generates multiple excerpt variations for blog posts with customizable
7-
/// length and writing style. Supports session-based usage (for UI with continuity)
8-
/// and one-shot generation (for tests and background tasks).
9-
@available(iOS 26, *)
4+
/// Excerpt generation for WordPress posts.
5+
///
6+
/// Generates multiple excerpt variations for blog posts with customizable
7+
/// length and writing style. Supports session-based usage (for UI with continuity)
8+
/// and one-shot generation (for tests and background tasks).
9+
@available(iOS 26, *)
1010
public struct ExcerptGeneration {
11-
public let length: ContentLength
12-
public let style: WritingStyle
11+
public var length: ContentLength
12+
public var style: WritingStyle
1313
public var options: GenerationOptions
1414

15-
public init(length: ContentLength, style: WritingStyle, options: GenerationOptions = GenerationOptions(temperature: 0.7)) {
15+
public init(
16+
length: ContentLength,
17+
style: WritingStyle,
18+
options: GenerationOptions = GenerationOptions(temperature: 0.7)
19+
) {
1620
self.length = length
1721
self.style = style
1822
self.options = options
1923
}
2024

21-
// MARK: - Instance Methods
25+
/// Generates excerpts with this configuration.
26+
public func generate(for content: String) async throws -> [String] {
27+
let content = IntelligenceService.extractRelevantText(from: content)
28+
let response = try await makeSession().respond(
29+
to: makePrompt(content: content),
30+
generating: Result.self,
31+
options: options
32+
)
33+
return response.content.excerpts
34+
}
2235

2336
/// Creates a language model session configured for excerpt generation.
2437
public func makeSession() -> LanguageModelSession {
@@ -28,63 +41,48 @@ public struct ExcerptGeneration {
2841
)
2942
}
3043

44+
/// Instructions for the language model session.
45+
public static var instructions: String {
46+
"""
47+
Generate exactly 3 excerpts for the blog post and follow the instructions from the prompt regarding the length and the style.
48+
49+
Generate excerpts in the same language as the POST_CONTENT.
50+
51+
**Paramters**
52+
- POST_CONTENT: contents of the post (HTML or plain text)
53+
- GENERATED_CONTENT_LENGTH: the length of the generated content
54+
- GENERATION_STYLE: the writing style to follow
55+
56+
**Requirements**
57+
- Each excerpt must follow the provided GENERATED_CONTENT_LENGTH and use GENERATION_STYLE
58+
59+
**Excerpt best practices**
60+
- Follow the best practices for post excerpts esteblished in the WordPress ecosystem
61+
- Include the post's main value proposition
62+
- Use active voice (avoid "is", "are", "was", "were" when possible)
63+
- End with implicit promise of more information
64+
- Do not use ellipsis (...) at the end
65+
- Focus on value, not summary
66+
- Include strategic keywords naturally
67+
- Write independently from the introduction – excerpt shouldn't just duplicate your opening paragraph. While your introduction eases readers into the topic, your excerpt needs to work as standalone copy that makes sense out of context—whether it appears in search results, social media cards, or email newsletters.
68+
"""
69+
}
70+
71+
3172
/// Creates a prompt for this excerpt configuration.
3273
public func makePrompt(content: String) -> String {
3374
"""
3475
Generate three different excerpts for the given post and parameters
35-
76+
3677
GENERATED_CONTENT_LENGTH: \(length.promptModifier)
37-
78+
3879
GENERATION_STYLE: \(style.promptModifier)
39-
80+
4081
POST_CONTENT: '''
4182
\(content)
4283
"""
4384
}
4485

45-
/// Generates excerpts with this configuration.
46-
public func generate(content: String) async throws -> [String] {
47-
let extractedContent = IntelligenceService.extractRelevantText(from: content)
48-
let session = makeSession()
49-
50-
let response = try await session.respond(
51-
to: makePrompt(content: extractedContent),
52-
generating: Result.self,
53-
options: options
54-
)
55-
56-
return response.content.excerpts
57-
}
58-
59-
// MARK: - Building Blocks (for UI with session continuity)
60-
61-
/// Instructions for the language model session.
62-
public static var instructions: String {
63-
"""
64-
Generate exactly 3 excerpts for the blog post and follow the instructions from the prompt regarding the length and the style.
65-
66-
Generate excerpts in the same language as the POST_CONTENT.
67-
68-
**Paramters**
69-
- POST_CONTENT: contents of the post (HTML or plain text)
70-
- GENERATED_CONTENT_LENGTH: the length of the generated content
71-
- GENERATION_STYLE: the writing style to follow
72-
73-
**Requirements**
74-
- Each excerpt must follow the provided GENERATED_CONTENT_LENGTH and use GENERATION_STYLE
75-
76-
**Excerpt best practices**
77-
- Follow the best practices for post excerpts esteblished in the WordPress ecosystem
78-
- Include the post's main value proposition
79-
- Use active voice (avoid "is", "are", "was", "were" when possible)
80-
- End with implicit promise of more information
81-
- Do not use ellipsis (...) at the end
82-
- Focus on value, not summary
83-
- Include strategic keywords naturally
84-
- Write independently from the introduction – excerpt shouldn't just duplicate your opening paragraph. While your introduction eases readers into the topic, your excerpt needs to work as standalone copy that makes sense out of context—whether it appears in search results, social media cards, or email newsletters.
85-
"""
86-
}
87-
8886
/// Prompt for generating additional excerpt options.
8987
public static var loadMorePrompt: String {
9088
"Generate additional three options"

Modules/Sources/WordPressIntelligence/UseCases/PostSummary.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extension IntelligenceService {
88
/// Generates concise summaries that capture the main points and key information
99
/// from WordPress post content in the same language as the source.
1010
public enum PostSummary {
11-
static func execute(content: String) -> LanguageModelSession.ResponseStream<String> {
11+
static func execute(content: String) async throws -> String {
1212
let content = IntelligenceService.extractRelevantText(from: content, ratio: 0.8)
1313

1414
let instructions = """
@@ -31,7 +31,7 @@ extension IntelligenceService {
3131
\(content)
3232
"""
3333

34-
return session.streamResponse(to: prompt)
34+
return try await session.respond(to: prompt).content
3535
}
3636
}
3737
}

Modules/Sources/WordPressIntelligence/IntelligenceUtilities.swift renamed to Modules/Sources/WordPressIntelligence/Utilities/ContentExtractor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22
import SwiftSoup
33

4-
public struct IntelligenceUtilities {
4+
public enum ContentExtractor {
55
/// Extracts semantically meaningful content from HTML for LLM processing.
66
///
77
/// Optimized for language models by:

Modules/Tests/WordPressIntelligenceTests/IntelligenceUtilitiesTests.swift renamed to Modules/Tests/WordPressIntelligenceTests/ContentExtractorTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import Testing
22
@testable import WordPressIntelligence
33

4-
struct IntelligenceUtilitiesTests {
4+
struct ContentExtractorTests {
55
@Test func extractRelevantText() throws {
6-
let text = try IntelligenceUtilities.extractRelevantText(from: IntelligenceUtilities.post)
6+
let text = try ContentExtractor.extractRelevantText(from: ContentExtractor.post)
77

88
#expect(text == """
99
<h1>The Art of Making Perfect Sourdough Bread at Home</h1>
@@ -52,7 +52,7 @@ struct IntelligenceUtilitiesTests {
5252

5353
/// Blockquote contain nested block and the implementation should account for that.
5454
@Test func blockquotes() throws {
55-
let text = try IntelligenceUtilities.extractRelevantText(from: """
55+
let text = try ContentExtractor.extractRelevantText(from: """
5656
<!-- wp:paragraph -->
5757
<p>Welcome to <strong><em>WordPress</em></strong>! This is your first post. Edit or delete it to take the first step in your blogging journey.</p>
5858
<!-- /wp:paragraph -->
@@ -71,13 +71,13 @@ struct IntelligenceUtilitiesTests {
7171
}
7272

7373
@Test func extractRelevantTextFromPlainText() throws {
74-
let text = try IntelligenceUtilities.extractRelevantText(from: "This is a plain text post")
74+
let text = try ContentExtractor.extractRelevantText(from: "This is a plain text post")
7575

7676
#expect(text == "This is a plain text post")
7777
}
7878
}
7979

80-
extension IntelligenceUtilities {
80+
extension ContentExtractor {
8181
static let post = """
8282
<!-- wp:heading {"level":1} -->
8383
<h1>The Art of Making Perfect Sourdough Bread at Home</h1>

0 commit comments

Comments
 (0)