diff --git a/Sources/asc-mcp/Services/JWTService.swift b/Sources/asc-mcp/Services/JWTService.swift index 33dd440..99a2f14 100644 --- a/Sources/asc-mcp/Services/JWTService.swift +++ b/Sources/asc-mcp/Services/JWTService.swift @@ -105,7 +105,7 @@ public actor JWTService { return Date(timeIntervalSince1970: TimeInterval(payload.exp)) } catch { #if DEBUG - print("🔐 JWT: Failed to decode token payload: \(error)") + print("🔐 JWT: Failed to decode token payload: \(error)", to: &standardError) #endif return nil } @@ -123,12 +123,12 @@ public actor JWTService { if timeUntilExpiry > tokenRefreshLeeway { // Token is still valid #if DEBUG - print("🔐 JWT: Using cached token (expires in \(Int(timeUntilExpiry))s)") + print("🔐 JWT: Using cached token (expires in \(Int(timeUntilExpiry))s)", to: &standardError) #endif return token } else { #if DEBUG - print("🔐 JWT: Token expires in \(Int(timeUntilExpiry))s, refreshing...") + print("🔐 JWT: Token expires in \(Int(timeUntilExpiry))s, refreshing...", to: &standardError) #endif } } @@ -140,7 +140,7 @@ public actor JWTService { #if DEBUG if let exp = decodeTokenExpiration(token) { let duration = exp.timeIntervalSince(now) - print("🔐 JWT: Generated new token (valid for \(Int(duration))s)") + print("🔐 JWT: Generated new token (valid for \(Int(duration))s)", to: &standardError) } #endif diff --git a/Sources/asc-mcp/Workers/SubscriptionsWorker/SubscriptionsWorker+RetentionHandlers.swift b/Sources/asc-mcp/Workers/SubscriptionsWorker/SubscriptionsWorker+RetentionHandlers.swift index a2d9812..c5f78f7 100644 --- a/Sources/asc-mcp/Workers/SubscriptionsWorker/SubscriptionsWorker+RetentionHandlers.swift +++ b/Sources/asc-mcp/Workers/SubscriptionsWorker/SubscriptionsWorker+RetentionHandlers.swift @@ -7,8 +7,16 @@ extension SubscriptionsWorker { private struct RetentionPaginationParams: Sendable { let limit: Int? let nextURL: String? + let scope: PaginationScope } + private static let retentionOfferPricesQueryParameters: [String: String] = [ + "include": "territory,subscriptionPricePoint", + "fields[retentionOfferPrices]": "territory,subscriptionPricePoint", + "fields[subscriptionPricePoints]": "customerPrice,proceeds,proceedsYear2,territory", + "fields[territories]": "currency" + ] + private static let allowedRetentionOfferModes: Set = [ "PAY_AS_YOU_GO", "PAY_UP_FRONT", @@ -49,16 +57,17 @@ extension SubscriptionsWorker { "app_id", "limit", "next_url" ]) let encodedAppId = try validateRetentionRequiredResourceID(params.arguments, key: "app_id") + let endpointPath = "/v1/apps/\(try ASCPathSegment.encode(encodedAppId, field: "app_id"))/retentionMessages" let pagination = try parseRetentionPagination( params.arguments, - allowedPaths: ["/v1/apps/\(try ASCPathSegment.encode(encodedAppId, field: "app_id"))/retentionMessages"] + endpointPath: endpointPath ) let response: ASCRetentionMessagesResponse if let nextURL = pagination.nextURL { - response = try await httpClient.get( + response = try await httpClient.getPage( nextURL, - parameters: [:], + scope: pagination.scope, as: ASCRetentionMessagesResponse.self ) } else { @@ -67,7 +76,7 @@ extension SubscriptionsWorker { queryParams["limit"] = String(limit) } response = try await httpClient.get( - "/v1/apps/\(try ASCPathSegment.encode(encodedAppId, field: "app_id"))/retentionMessages", + endpointPath, parameters: queryParams, as: ASCRetentionMessagesResponse.self ) @@ -192,19 +201,17 @@ extension SubscriptionsWorker { params.arguments, key: "retention_message_id" ) + let endpointPath = "/v1/retentionMessages/\(try ASCPathSegment.encode(encodedMessageId, field: "retention_message_id"))/versions" let pagination = try parseRetentionPagination( params.arguments, - allowedPaths: [ - "/v1/retentionMessages/\(try ASCPathSegment.encode(encodedMessageId, field: "retention_message_id"))/versions", - "/v1/retentionMessages/\(try ASCPathSegment.encode(encodedMessageId, field: "retention_message_id"))/retentionMessageVersions" - ] + endpointPath: endpointPath ) let response: ASCRetentionMessageVersionsResponse if let nextURL = pagination.nextURL { - response = try await httpClient.get( + response = try await httpClient.getPage( nextURL, - parameters: [:], + scope: pagination.scope, as: ASCRetentionMessageVersionsResponse.self ) } else { @@ -213,7 +220,7 @@ extension SubscriptionsWorker { queryParams["limit"] = String(limit) } response = try await httpClient.get( - "/v1/retentionMessages/\(try ASCPathSegment.encode(encodedMessageId, field: "retention_message_id"))/versions", + endpointPath, parameters: queryParams, as: ASCRetentionMessageVersionsResponse.self ) @@ -313,19 +320,17 @@ extension SubscriptionsWorker { params.arguments, key: "version_id" ) + let endpointPath = "/v1/retentionMessageVersions/\(try ASCPathSegment.encode(encodedVersionId, field: "version_id"))/localizations" let pagination = try parseRetentionPagination( params.arguments, - allowedPaths: [ - "/v1/retentionMessageVersions/\(try ASCPathSegment.encode(encodedVersionId, field: "version_id"))/localizations", - "/v1/retentionMessageVersions/\(try ASCPathSegment.encode(encodedVersionId, field: "version_id"))/retentionMessageLocalizations" - ] + endpointPath: endpointPath ) let response: ASCRetentionMessageLocalizationsResponse if let nextURL = pagination.nextURL { - response = try await httpClient.get( + response = try await httpClient.getPage( nextURL, - parameters: [:], + scope: pagination.scope, as: ASCRetentionMessageLocalizationsResponse.self ) } else { @@ -334,7 +339,7 @@ extension SubscriptionsWorker { queryParams["limit"] = String(limit) } response = try await httpClient.get( - "/v1/retentionMessageVersions/\(try ASCPathSegment.encode(encodedVersionId, field: "version_id"))/localizations", + endpointPath, parameters: queryParams, as: ASCRetentionMessageLocalizationsResponse.self ) @@ -538,19 +543,17 @@ extension SubscriptionsWorker { params.arguments, key: "retention_message_id" ) + let endpointPath = "/v1/retentionMessages/\(try ASCPathSegment.encode(encodedMessageId, field: "retention_message_id"))/subscriptions" let pagination = try parseRetentionPagination( params.arguments, - allowedPaths: [ - "/v1/retentionMessages/\(try ASCPathSegment.encode(encodedMessageId, field: "retention_message_id"))/subscriptions", - "/v1/retentionMessages/\(try ASCPathSegment.encode(encodedMessageId, field: "retention_message_id"))/relationships/subscriptions" - ] + endpointPath: endpointPath ) let response: ASCRetentionMessageSubscriptionsResponse if let nextURL = pagination.nextURL { - response = try await httpClient.get( + response = try await httpClient.getPage( nextURL, - parameters: [:], + scope: pagination.scope, as: ASCRetentionMessageSubscriptionsResponse.self ) } else { @@ -559,7 +562,7 @@ extension SubscriptionsWorker { queryParams["limit"] = String(limit) } response = try await httpClient.get( - "/v1/retentionMessages/\(try ASCPathSegment.encode(encodedMessageId, field: "retention_message_id"))/subscriptions", + endpointPath, parameters: queryParams, as: ASCRetentionMessageSubscriptionsResponse.self ) @@ -616,17 +619,14 @@ extension SubscriptionsWorker { let pagination = try parseRetentionPagination( params.arguments, - allowedPaths: [ - "/v1/subscriptions/\(try ASCPathSegment.encode(encodedSubId, field: "subscription_id"))/retentionOffers", - "/v1/subscriptions/\(try ASCPathSegment.encode(encodedSubId, field: "subscription_id"))/retentionMessageOffers" - ] + endpointPath: endpointPath ) let response: ASCRetentionOffersResponse if let nextURL = pagination.nextURL { - response = try await httpClient.get( + response = try await httpClient.getPage( nextURL, - parameters: [:], + scope: pagination.scope, as: ASCRetentionOffersResponse.self ) } else { @@ -924,30 +924,29 @@ extension SubscriptionsWorker { key: "offer_id" ) let territories = try parseOptionalTerritoryFilter(params.arguments, key: "territory") + let endpointPath = "/v1/retentionOffers/\(try ASCPathSegment.encode(encodedOfferId, field: "offer_id"))/prices" + + var requiredPaginationParams: [String: String] = [:] + if let territories, !territories.isEmpty { + requiredPaginationParams["filter[territory]"] = territories.joined(separator: ",") + } let pagination = try parseRetentionPagination( params.arguments, - allowedPaths: ["/v1/retentionOffers/\(try ASCPathSegment.encode(encodedOfferId, field: "offer_id"))/prices"], - allowedQueryKeys: [ - "limit", - "cursor", - "filter[territory]", - "include", - "fields[retentionOfferPrices]", - "fields[territories]", - "fields[subscriptionPricePoints]" - ] + endpointPath: endpointPath, + requiredParameters: requiredPaginationParams, + optionalFixedParameters: Self.retentionOfferPricesQueryParameters ) let response: ASCRetentionOfferPricesResponse if let nextURL = pagination.nextURL { - response = try await httpClient.get( + response = try await httpClient.getPage( nextURL, - parameters: [:], + scope: pagination.scope, as: ASCRetentionOfferPricesResponse.self ) } else { - var queryParams: [String: String] = [:] + var queryParams = Self.retentionOfferPricesQueryParameters if let limit = pagination.limit { queryParams["limit"] = String(limit) } @@ -955,13 +954,13 @@ extension SubscriptionsWorker { queryParams["filter[territory]"] = territories.joined(separator: ",") } response = try await httpClient.get( - "/v1/retentionOffers/\(try ASCPathSegment.encode(encodedOfferId, field: "offer_id"))/prices", + endpointPath, parameters: queryParams, as: ASCRetentionOfferPricesResponse.self ) } - let prices = response.data.map { formatRetentionOfferPrice($0) } + let prices = response.data.map { formatRetentionOfferPrice($0, included: response.included) } var result: [String: Any] = [ "success": true, "data": prices, @@ -1192,8 +1191,9 @@ extension SubscriptionsWorker { private func parseRetentionPagination( _ arguments: [String: Value]?, - allowedPaths: Set, - allowedQueryKeys: Set = ["limit", "cursor", "include"] + endpointPath: String, + requiredParameters: [String: String] = [:], + optionalFixedParameters: [String: String] = [:] ) throws -> RetentionPaginationParams { var parsedLimit: Int? if let rawLimit = arguments?["limit"], rawLimit != .null { @@ -1203,6 +1203,15 @@ extension SubscriptionsWorker { parsedLimit = intLimit } + var effectiveRequired = requiredParameters + if let parsedLimit { + effectiveRequired["limit"] = String(parsedLimit) + } + + var allowedKeys: Set = ["cursor", "limit"] + allowedKeys.formUnion(effectiveRequired.keys) + allowedKeys.formUnion(optionalFixedParameters.keys) + var parsedNextURL: String? if let rawNext = arguments?["next_url"], rawNext != .null { guard case .string(let nextString) = rawNext else { @@ -1219,46 +1228,39 @@ extension SubscriptionsWorker { throw ASCError.parsing("Invalid 'next_url' format") } - if let scheme = components.scheme { - guard scheme.lowercased() == "https", - components.host?.lowercased() == "api.appstoreconnect.apple.com", - components.port == nil else { - throw ASCError.parsing( - "Invalid 'next_url': must target https://api.appstoreconnect.apple.com" - ) - } - } else if components.host != nil { - throw ASCError.parsing("Invalid 'next_url' host") - } - - let path = components.percentEncodedPath - guard allowedPaths.contains(path) else { - throw ASCError.parsing( - "Invalid 'next_url' path '\(path)' for this endpoint" - ) - } - if let queryItems = components.queryItems { for item in queryItems { - guard allowedQueryKeys.contains(item.name) else { + guard allowedKeys.contains(item.name) else { throw ASCError.parsing( "Unsupported query parameter '\(item.name)' in 'next_url'" ) } - if item.name == "limit", let val = item.value { - guard let intLimit = Int(val), (1...200).contains(intLimit) else { + if item.name == "limit" { + guard let val = item.value, + let intLimit = Int(val), + (1...200).contains(intLimit) else { throw ASCError.parsing( "Invalid 'limit' in 'next_url': must be between 1 and 200" ) } } + if let expectedFixedValue = optionalFixedParameters[item.name] { + effectiveRequired[item.name] = expectedFixedValue + } } } parsedNextURL = nextString } - return RetentionPaginationParams(limit: parsedLimit, nextURL: parsedNextURL) + let scope = PaginationScope( + path: endpointPath, + requiredParameters: effectiveRequired, + allowedParameters: allowedKeys, + requiredNonEmptyParameters: ["cursor"] + ) + + return RetentionPaginationParams(limit: parsedLimit, nextURL: parsedNextURL, scope: scope) } private func validateRetentionReferenceName( @@ -1351,13 +1353,7 @@ extension SubscriptionsWorker { } private func validateNoEmoji(in text: String, field: String) throws { - let containsEmoji = text.unicodeScalars.contains { scalar in - scalar.properties.isEmojiPresentation - || scalar.value >= 0x1F000 - || (0x2600...0x27BF).contains(scalar.value) - || scalar.value == 0xFE0F - } - if containsEmoji { + if ASCMetadataValidator.containsEmoji(text) { throw ASCError.parsing("Parameter '\(field)' must not contain emoji characters") } } @@ -1785,16 +1781,46 @@ extension SubscriptionsWorker { return result } - private func formatRetentionOfferPrice(_ price: ASCRetentionOfferPrice) -> [String: Any] { + private func formatRetentionOfferPrice( + _ price: ASCRetentionOfferPrice, + included: [ASCWinBackOfferPriceIncludedResource]? = nil + ) -> [String: Any] { var result: [String: Any] = [ "id": price.id, "type": price.type ] - if let territoryId = price.relationships?.territory?.data?.id { + let pricePointId = price.relationships?.subscriptionPricePoint?.data?.id + let pricePoint = pricePointId.flatMap { id in + included?.first(where: { $0.type == "subscriptionPricePoints" && $0.id == id }) + } + let territoryId = price.relationships?.territory?.data?.id + ?? pricePoint?.relationships?["territory"]?.objectValue?["data"]?.objectValue?["id"]?.stringValue + let territory = territoryId.flatMap { id in + included?.first(where: { $0.type == "territories" && $0.id == id }) + } + + if let territoryId { result["territoryId"] = territoryId + result["territory_id"] = territoryId } - if let pricePointId = price.relationships?.subscriptionPricePoint?.data?.id { + if let pricePointId { result["subscriptionPricePointId"] = pricePointId + result["subscription_price_point_id"] = pricePointId + result["price_point_id"] = pricePointId + } + if let currency = territory?.attributes?["currency"]?.stringValue { + result["currency"] = currency + } + if let customerPrice = pricePoint?.attributes?["customerPrice"]?.stringValue { + result["customerPrice"] = customerPrice + result["customer_price"] = customerPrice + } + if let proceeds = pricePoint?.attributes?["proceeds"]?.stringValue { + result["proceeds"] = proceeds + } + if let proceedsYear2 = pricePoint?.attributes?["proceedsYear2"]?.stringValue { + result["proceedsYear2"] = proceedsYear2 + result["proceeds_year2"] = proceedsYear2 } return result } diff --git a/Tests/ASCMCPTests/Workers/RetentionMessagingContractTests.swift b/Tests/ASCMCPTests/Workers/RetentionMessagingContractTests.swift index 4b1aac6..9f3b1aa 100644 --- a/Tests/ASCMCPTests/Workers/RetentionMessagingContractTests.swift +++ b/Tests/ASCMCPTests/Workers/RetentionMessagingContractTests.swift @@ -404,7 +404,21 @@ struct RetentionMessagingContractTests { arguments: ["offer_id": .string("ro-1"), "territory": .string("USA,GBR")] )) #expect(prices.isError != true) - #expect(try object(prices.structuredContent)["count"] == .int(1)) + let pricesPayload = try object(prices.structuredContent) + #expect(pricesPayload["count"] == .int(1)) + let firstPrice = try object(try #require(pricesPayload["prices"]?.arrayValue?.first)) + #expect(firstPrice["id"] == .string("rop-1")) + #expect(firstPrice["territoryId"] == .string("USA")) + #expect(firstPrice["territory_id"] == .string("USA")) + #expect(firstPrice["subscriptionPricePointId"] == .string("pp-usa-1")) + #expect(firstPrice["subscription_price_point_id"] == .string("pp-usa-1")) + #expect(firstPrice["price_point_id"] == .string("pp-usa-1")) + #expect(firstPrice["currency"] == .string("USD")) + #expect(firstPrice["customerPrice"] == .string("4.99")) + #expect(firstPrice["customer_price"] == .string("4.99")) + #expect(firstPrice["proceeds"] == .string("3.49")) + #expect(firstPrice["proceedsYear2"] == .string("4.24")) + #expect(firstPrice["proceeds_year2"] == .string("4.24")) let deleted = try await worker.handleTool(.init( name: "subscriptions_delete_retention_offer", @@ -423,6 +437,14 @@ struct RetentionMessagingContractTests { #expect(patchAttributes["eligibilitySubscriberDurationInMonths"] == nil) #expect(patchAttributes["waitBetweenOffersInMonths"] == nil) #expect(requests[5].url?.path == "/v1/retentionOffers/ro-1/prices") + let pricesRequestURL = try #require(requests[5].url) + let pricesQueryItems = try #require(URLComponents(url: pricesRequestURL, resolvingAgainstBaseURL: false)?.queryItems) + let pricesQuery = Dictionary(uniqueKeysWithValues: pricesQueryItems.map { ($0.name, $0.value ?? "") }) + #expect(pricesQuery["filter[territory]"] == "USA,GBR") + #expect(pricesQuery["include"] == "territory,subscriptionPricePoint") + #expect(pricesQuery["fields[retentionOfferPrices]"] == "territory,subscriptionPricePoint") + #expect(pricesQuery["fields[subscriptionPricePoints]"] == "customerPrice,proceeds,proceedsYear2,territory") + #expect(pricesQuery["fields[territories]"] == "currency") } // MARK: - 4. Subscription Retention Relationships @@ -655,6 +677,358 @@ struct RetentionMessagingContractTests { #expect(item["resource_jsonapi_type"] == .string("retentionMessageVersions")) #expect(item["resource_id"] == .string("ver-1")) } + + // MARK: - 7. Retention List Pagination (All 14 Combinations + Scope Rejections) + + @Test("retention list handlers support absolute and root-relative next_url across all 7 endpoints and reject out-of-scope pagination links") + func retentionListPaginationAllCombinationsAndRejections() async throws { + let messagesDoc = #"{"data":[{"type":"retentionMessages","id":"msg-2","attributes":{"referenceName":"Page 2"}}],"links":{"self":"https://api.example.test/v1/apps/app-1/retentionMessages?cursor=page-2"}}"# + let versionsDoc = #"{"data":[{"type":"retentionMessageVersions","id":"ver-2","attributes":{"version":2,"state":"READY_FOR_REVIEW"}}],"links":{"self":"https://api.example.test/v1/retentionMessages/msg-1/versions?cursor=page-2"}}"# + let localizationsDoc = #"{"data":[{"type":"retentionMessageLocalizations","id":"loc-2","attributes":{"locale":"ja","header":"継続特典","body":"すべての機能を引き続きご利用いただけます。"}}],"links":{"self":"https://api.example.test/v1/retentionMessageVersions/ver-1/localizations?cursor=page-2"}}"# + let subscriptionsDoc = #"{"data":[{"type":"subscriptions","id":"sub-2","attributes":{"name":"Pro Annual","productId":"com.example.annual","state":"APPROVED","subscriptionPeriod":"ONE_YEAR"}}],"links":{"self":"https://api.example.test/v1/retentionMessages/msg-1/subscriptions?cursor=page-2"}}"# + let offersDoc = #"{"data":[{"type":"retentionOffers","id":"ro-2","attributes":{"referenceName":"Page 2 Offer","offerId":"OFFER2","duration":"ONE_MONTH","offerMode":"PAY_AS_YOU_GO","periodCount":1,"customerEligibilityPaidSubscriptionDurationInMonths":6}}],"links":{"self":"https://api.example.test/v1/subscriptions/sub-1/retentionOffers?cursor=page-2"}}"# + let messageOffersDoc = #"{"data":[{"type":"retentionOffers","id":"ro-3","attributes":{"referenceName":"Page 2 Message Offer","offerId":"MSGOFFER2","duration":"ONE_MONTH","offerMode":"PAY_AS_YOU_GO","periodCount":1,"customerEligibilityPaidSubscriptionDurationInMonths":6}}],"links":{"self":"https://api.example.test/v1/subscriptions/sub-1/retentionMessageOffers?cursor=page-2"}}"# + let pricesDoc = #"{"data":[{"type":"retentionOfferPrices","id":"rop-2","relationships":{"territory":{"data":{"type":"territories","id":"USA"}},"subscriptionPricePoint":{"data":{"type":"subscriptionPricePoints","id":"pp-usa-2"}}}}],"included":[{"type":"subscriptionPricePoints","id":"pp-usa-2","attributes":{"customerPrice":"2.99","proceeds":"2.10","proceedsYear2":"2.54"}},{"type":"territories","id":"USA","attributes":{"currency":"USD"}}],"links":{"self":"https://api.example.test/v1/retentionOffers/ro-1/prices?cursor=page-2"}}"# + + // 1. Negative checks before any HTTP I/O + let negativeTransport = TestHTTPTransport(responses: []) + let negativeWorker = try await makeSubscriptionsWorker(transport: negativeTransport) + + let negativeCases: [(String, [String: Value])] = [ + // Wrong parent ID + ( + "subscriptions_list_retention_messages", + ["app_id": .string("app-1"), "next_url": .string("https://api.example.test/v1/apps/app-other/retentionMessages?cursor=page-2")] + ), + // Wrong endpoint (retentionOffers when message_offers_only is true) + ( + "subscriptions_list_retention_offers", + [ + "subscription_id": .string("sub-1"), + "message_offers_only": .bool(true), + "next_url": .string("https://api.example.test/v1/subscriptions/sub-1/retentionOffers?cursor=page-2") + ] + ), + // Wrong endpoint (retentionMessageOffers when message_offers_only is false) + ( + "subscriptions_list_retention_offers", + [ + "subscription_id": .string("sub-1"), + "message_offers_only": .bool(false), + "next_url": .string("/v1/subscriptions/sub-1/retentionMessageOffers?cursor=page-2") + ] + ), + // Missing cursor + ( + "subscriptions_list_retention_message_versions", + ["retention_message_id": .string("msg-1"), "next_url": .string("https://api.example.test/v1/retentionMessages/msg-1/versions?limit=10")] + ), + // Empty cursor + ( + "subscriptions_list_retention_message_localizations", + ["version_id": .string("ver-1"), "next_url": .string("/v1/retentionMessageVersions/ver-1/localizations?cursor=")] + ), + // Unrequested filter[territory] when territory argument was omitted + ( + "subscriptions_list_retention_offer_prices", + ["offer_id": .string("ro-1"), "next_url": .string("https://api.example.test/v1/retentionOffers/ro-1/prices?cursor=page-2&filter%5Bterritory%5D=USA")] + ), + // Changed filter[territory] when territory argument was provided + ( + "subscriptions_list_retention_offer_prices", + [ + "offer_id": .string("ro-1"), + "territory": .string("USA"), + "next_url": .string("https://api.example.test/v1/retentionOffers/ro-1/prices?cursor=page-2&filter%5Bterritory%5D=GBR") + ] + ), + // Missing filter[territory] in next_url when territory argument was provided + ( + "subscriptions_list_retention_offer_prices", + [ + "offer_id": .string("ro-1"), + "territory": .string("USA"), + "next_url": .string("/v1/retentionOffers/ro-1/prices?cursor=page-2") + ] + ), + // Tampered include in next_url + ( + "subscriptions_list_retention_offer_prices", + [ + "offer_id": .string("ro-1"), + "next_url": .string("https://api.example.test/v1/retentionOffers/ro-1/prices?cursor=page-2&include=subscription") + ] + ) + ] + + for (toolName, arguments) in negativeCases { + let result = try await negativeWorker.handleTool(.init(name: toolName, arguments: arguments)) + #expect(result.isError == true, "Expected \(toolName) with \(arguments) to be rejected before HTTP I/O") + } + #expect(await negativeTransport.requestCount() == 0) + + // 2. Positive checks across all 14 combinations (7 endpoints x absolute + root-relative) plus filtered prices + let transport = TestHTTPTransport(responses: [ + .init(statusCode: 200, body: messagesDoc), + .init(statusCode: 200, body: messagesDoc), + .init(statusCode: 200, body: versionsDoc), + .init(statusCode: 200, body: versionsDoc), + .init(statusCode: 200, body: localizationsDoc), + .init(statusCode: 200, body: localizationsDoc), + .init(statusCode: 200, body: subscriptionsDoc), + .init(statusCode: 200, body: subscriptionsDoc), + .init(statusCode: 200, body: offersDoc), + .init(statusCode: 200, body: offersDoc), + .init(statusCode: 200, body: messageOffersDoc), + .init(statusCode: 200, body: messageOffersDoc), + .init(statusCode: 200, body: pricesDoc), + .init(statusCode: 200, body: pricesDoc), + .init(statusCode: 200, body: pricesDoc) + ]) + let worker = try await makeSubscriptionsWorker(transport: transport) + + let positiveCases: [(String, [String: Value], String)] = [ + // 1-2: subscriptions_list_retention_messages + ( + "subscriptions_list_retention_messages", + ["app_id": .string("app-1"), "next_url": .string("https://api.example.test/v1/apps/app-1/retentionMessages?cursor=page-2")], + "/v1/apps/app-1/retentionMessages" + ), + ( + "subscriptions_list_retention_messages", + ["app_id": .string("app-1"), "next_url": .string("/v1/apps/app-1/retentionMessages?cursor=page-2")], + "/v1/apps/app-1/retentionMessages" + ), + // 3-4: subscriptions_list_retention_message_versions + ( + "subscriptions_list_retention_message_versions", + ["retention_message_id": .string("msg-1"), "next_url": .string("https://api.example.test/v1/retentionMessages/msg-1/versions?cursor=page-2")], + "/v1/retentionMessages/msg-1/versions" + ), + ( + "subscriptions_list_retention_message_versions", + ["retention_message_id": .string("msg-1"), "next_url": .string("/v1/retentionMessages/msg-1/versions?cursor=page-2")], + "/v1/retentionMessages/msg-1/versions" + ), + // 5-6: subscriptions_list_retention_message_localizations + ( + "subscriptions_list_retention_message_localizations", + ["version_id": .string("ver-1"), "next_url": .string("https://api.example.test/v1/retentionMessageVersions/ver-1/localizations?cursor=page-2")], + "/v1/retentionMessageVersions/ver-1/localizations" + ), + ( + "subscriptions_list_retention_message_localizations", + ["version_id": .string("ver-1"), "next_url": .string("/v1/retentionMessageVersions/ver-1/localizations?cursor=page-2")], + "/v1/retentionMessageVersions/ver-1/localizations" + ), + // 7-8: subscriptions_list_retention_message_subscriptions + ( + "subscriptions_list_retention_message_subscriptions", + ["retention_message_id": .string("msg-1"), "next_url": .string("https://api.example.test/v1/retentionMessages/msg-1/subscriptions?cursor=page-2")], + "/v1/retentionMessages/msg-1/subscriptions" + ), + ( + "subscriptions_list_retention_message_subscriptions", + ["retention_message_id": .string("msg-1"), "next_url": .string("/v1/retentionMessages/msg-1/subscriptions?cursor=page-2")], + "/v1/retentionMessages/msg-1/subscriptions" + ), + // 9-10: subscriptions_list_retention_offers (message_offers_only: false) + ( + "subscriptions_list_retention_offers", + [ + "subscription_id": .string("sub-1"), + "message_offers_only": .bool(false), + "next_url": .string("https://api.example.test/v1/subscriptions/sub-1/retentionOffers?cursor=page-2") + ], + "/v1/subscriptions/sub-1/retentionOffers" + ), + ( + "subscriptions_list_retention_offers", + [ + "subscription_id": .string("sub-1"), + "message_offers_only": .bool(false), + "next_url": .string("/v1/subscriptions/sub-1/retentionOffers?cursor=page-2") + ], + "/v1/subscriptions/sub-1/retentionOffers" + ), + // 11-12: subscriptions_list_retention_offers (message_offers_only: true) + ( + "subscriptions_list_retention_offers", + [ + "subscription_id": .string("sub-1"), + "message_offers_only": .bool(true), + "next_url": .string("https://api.example.test/v1/subscriptions/sub-1/retentionMessageOffers?cursor=page-2") + ], + "/v1/subscriptions/sub-1/retentionMessageOffers" + ), + ( + "subscriptions_list_retention_offers", + [ + "subscription_id": .string("sub-1"), + "message_offers_only": .bool(true), + "next_url": .string("/v1/subscriptions/sub-1/retentionMessageOffers?cursor=page-2") + ], + "/v1/subscriptions/sub-1/retentionMessageOffers" + ), + // 13-14: subscriptions_list_retention_offer_prices (absolute with full query + root-relative minimal query) + ( + "subscriptions_list_retention_offer_prices", + [ + "offer_id": .string("ro-1"), + "next_url": .string("https://api.example.test/v1/retentionOffers/ro-1/prices?cursor=page-2&include=territory,subscriptionPricePoint&fields%5BretentionOfferPrices%5D=territory,subscriptionPricePoint&fields%5BsubscriptionPricePoints%5D=customerPrice,proceeds,proceedsYear2,territory&fields%5Bterritories%5D=currency") + ], + "/v1/retentionOffers/ro-1/prices" + ), + ( + "subscriptions_list_retention_offer_prices", + [ + "offer_id": .string("ro-1"), + "next_url": .string("/v1/retentionOffers/ro-1/prices?cursor=page-2") + ], + "/v1/retentionOffers/ro-1/prices" + ), + // 15: subscriptions_list_retention_offer_prices with territory filter preserved in next_url + ( + "subscriptions_list_retention_offer_prices", + [ + "offer_id": .string("ro-1"), + "territory": .string("USA,GBR"), + "next_url": .string("https://api.example.test/v1/retentionOffers/ro-1/prices?cursor=page-2&filter%5Bterritory%5D=GBR,USA") + ], + "/v1/retentionOffers/ro-1/prices" + ) + ] + + for (index, (toolName, arguments, _)) in positiveCases.enumerated() { + let result = try await worker.handleTool(.init(name: toolName, arguments: arguments)) + #expect(result.isError != true, "Case \(index) (\(toolName)) failed: \(result.content)") + let payload = try object(result.structuredContent) + #expect(payload["count"] == .int(1)) + } + + let requests = await transport.recordedRequests() + #expect(requests.count == 15) + for (index, (_, _, expectedPath)) in positiveCases.enumerated() { + #expect(requests[index].url?.path == expectedPath) + #expect(requests[index].url?.query?.contains("cursor=page-2") == true) + } + } + + // MARK: - 8. Plane 2 CJK Ideographs vs Emoji Validation + + @Test("retention copy and reference_name validation accepts Plane 2 CJK ideographs and rejects emojis") + func plane2CJKAcceptedAndEmojisRejected() async throws { + let transport = TestHTTPTransport(responses: [ + .init( + statusCode: 201, + body: #"{"data":{"type":"retentionMessageLocalizations","id":"loc-ja-1","attributes":{"locale":"ja","header":"𠮷野の便利な機能を継続","body":"𠮟咤激励の機能も引き続きご利用いただけます。"},"relationships":{"retentionMessageVersion":{"data":{"type":"retentionMessageVersions","id":"ver-1"}}}}}"# + ), + .init( + statusCode: 200, + body: #"{"data":{"type":"retentionMessageLocalizations","id":"loc-ja-1","attributes":{"locale":"ja","header":"𠮷野の便利な機能を継続","body":"𠮟咤激励の機能も引き続きご利用いただけます。"}}}"# + ), + .init( + statusCode: 201, + body: #"{"data":{"type":"retentionMessages","id":"msg-ja-1","attributes":{"referenceName":"𠮷野キャンペーン"},"relationships":{"app":{"data":{"type":"apps","id":"app-1"}}}}}"# + ), + .init( + statusCode: 201, + body: #"{"data":{"type":"retentionOffers","id":"ro-ja-1","attributes":{"referenceName":"𠮷野キャンペーン","offerId":"YOSHINO1","duration":"ONE_MONTH","offerMode":"PAY_AS_YOU_GO","periodCount":1,"customerEligibilityPaidSubscriptionDurationInMonths":6}}}"# + ) + ]) + let worker = try await makeSubscriptionsWorker(transport: transport) + + // Real emojis rejected before network access + for emojiHeader in ["キャンペーン🎉", "特典🔥", "継続✨", "限定❤️", "夏休み☀️"] { + let rejected = try await worker.handleTool(.init( + name: "subscriptions_create_retention_message_localization", + arguments: [ + "version_id": .string("ver-1"), + "locale": .string("ja"), + "header": .string(emojiHeader), + "body": .string("引き続きご利用いただけます。") + ] + )) + #expect(rejected.isError == true, "Expected emoji header '\(emojiHeader)' to be rejected") + } + + let rejectedMessageRef = try await worker.handleTool(.init( + name: "subscriptions_create_retention_message", + arguments: [ + "app_id": .string("app-1"), + "reference_name": .string("キャンペーン🔥") + ] + )) + #expect(rejectedMessageRef.isError == true) + + let rejectedOfferRef = try await worker.handleTool(.init( + name: "subscriptions_create_retention_offer", + arguments: [ + "subscription_id": .string("sub-1"), + "reference_name": .string("キャンペーン🔥"), + "offer_id": .string("YOSHINO1"), + "offer_mode": .string("PAY_AS_YOU_GO"), + "duration": .string("ONE_MONTH"), + "period_count": .int(1), + "eligibility_duration_months": .int(6), + "price_point_ids": .array([.string("pp-usa-1")]), + "territory_ids": .array([.string("USA")]) + ] + )) + #expect(rejectedOfferRef.isError == true) + #expect(await transport.requestCount() == 0) + + // Plane 2 CJK Unified Ideographs Extension B (𠮷 U+20BB7, 𠮟 U+20B9F) accepted + let createdLoc = try await worker.handleTool(.init( + name: "subscriptions_create_retention_message_localization", + arguments: [ + "version_id": .string("ver-1"), + "locale": .string("ja"), + "header": .string("𠮷野の便利な機能を継続"), + "body": .string("𠮟咤激励の機能も引き続きご利用いただけます。") + ] + )) + #expect(createdLoc.isError != true) + let locPayload = try object(try object(createdLoc.structuredContent)["localization"]) + #expect(locPayload["header"] == .string("𠮷野の便利な機能を継続")) + #expect(locPayload["body"] == .string("𠮟咤激励の機能も引き続きご利用いただけます。")) + + let updatedLoc = try await worker.handleTool(.init( + name: "subscriptions_update_retention_message_localization", + arguments: [ + "localization_id": .string("loc-ja-1"), + "header": .string("𠮷野の便利な機能を継続"), + "body": .string("𠮟咤激励の機能も引き続きご利用いただけます。") + ] + )) + #expect(updatedLoc.isError != true) + + let createdMessage = try await worker.handleTool(.init( + name: "subscriptions_create_retention_message", + arguments: [ + "app_id": .string("app-1"), + "reference_name": .string("𠮷野キャンペーン") + ] + )) + #expect(createdMessage.isError != true) + + let createdOffer = try await worker.handleTool(.init( + name: "subscriptions_create_retention_offer", + arguments: [ + "subscription_id": .string("sub-1"), + "reference_name": .string("𠮷野キャンペーン"), + "offer_id": .string("YOSHINO1"), + "offer_mode": .string("PAY_AS_YOU_GO"), + "duration": .string("ONE_MONTH"), + "period_count": .int(1), + "eligibility_duration_months": .int(6), + "price_point_ids": .array([.string("pp-usa-1")]), + "territory_ids": .array([.string("USA")]) + ] + )) + #expect(createdOffer.isError != true) + #expect(await transport.requestCount() == 4) + } } private func makeSubscriptionsWorker(transport: TestHTTPTransport) async throws -> SubscriptionsWorker {