Files
iOS/Tests/Shared/CallServiceResponse.test.swift
Bruno Pantaleão Gonçalves 00d3852f22 Improve "Perform action" App Intent (#4720)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->
This PR adds the capability of receive a response from the perform
action App Intent + some small improvements on actions list rendering

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->
<img width="1792" height="1550" alt="CleanShot 2026-06-09 at 21 06
09@2x"
src="https://github.com/user-attachments/assets/3036b9cd-0992-46d3-840f-275725a543ac"
/>

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 09:13:32 +02:00

74 lines
2.5 KiB
Swift

import Foundation
import HAKit
@testable import Shared
import Testing
struct CallServiceResponseTests {
@Test func decodesResponseDictionary() throws {
let data = HAData(value: [
"context": ["id": "abc"],
"response": ["forecast": ["temperature": 21]],
])
let response = try CallServiceResponse(data: data)
#expect(response.hasResponse == true)
let dictionary = try #require(response.response as? [String: Any])
#expect(dictionary["forecast"] != nil)
}
@Test func noResponseKeyHasNoResponse() throws {
let data = HAData(value: ["context": ["id": "abc"]])
let response = try CallServiceResponse(data: data)
#expect(response.hasResponse == false)
#expect(response.jsonString() == nil)
}
@Test func emptyResponseDictionaryHasNoResponse() throws {
let data = HAData(value: ["context": [:], "response": [String: Any]()])
let response = try CallServiceResponse(data: data)
#expect(response.hasResponse == false)
#expect(response.jsonString() == nil)
}
@Test func nullResponseHasNoResponse() throws {
let data = HAData(value: ["response": NSNull()])
let response = try CallServiceResponse(data: data)
#expect(response.hasResponse == false)
#expect(response.jsonString() == nil)
}
@Test func nonDictionaryDataHasNoResponse() throws {
let response = try CallServiceResponse(data: HAData(value: "ok"))
#expect(response.hasResponse == false)
#expect(response.jsonString() == nil)
}
@Test func serializesDictionaryResponseToJSON() throws {
let data = HAData(value: ["response": ["count": 2, "name": "kitchen"]])
let response = try CallServiceResponse(data: data)
let json = try #require(response.jsonString())
// sortedKeys makes the output deterministic.
#expect(json == "{\"count\":2,\"name\":\"kitchen\"}")
}
@Test func serializesArrayResponseToJSON() throws {
let data = HAData(value: ["response": [1, 2, 3]])
let response = try CallServiceResponse(data: data)
#expect(response.jsonString() == "[1,2,3]")
}
@Test func wrapsPrimitiveResponseForSerialization() throws {
let data = HAData(value: ["response": "hello"])
let response = try CallServiceResponse(data: data)
#expect(response.hasResponse == true)
#expect(response.jsonString() == "{\"response\":\"hello\"}")
}
}