Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomvis committed Sep 11, 2023
1 parent acc64ca commit 3cda9cb
Show file tree
Hide file tree
Showing 30 changed files with 2,513 additions and 3,641 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "cef5b3f6f11781dd4591bdd1dd0a3d22bd609334",
"version" : "1.11.0"
"revision" : "26ed3a2b4a2df47917ca9b790a57f91285b923fb",
"version" : "1.12.0"
}
},
{
Expand Down
10 changes: 10 additions & 0 deletions App/Construct.xcodeproj/xcshareddata/xcschemes/Construct.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@
ReferencedContainer = "container:..">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CompendiumTests"
BuildableName = "CompendiumTests"
BlueprintName = "CompendiumTests"
ReferencedContainer = "container:..">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
18 changes: 13 additions & 5 deletions App/UnitTests/CompendiumImporterTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CompendiumImporterTest: XCTestCase {
sourceId: .defaultMonsters,
sourceVersion: nil,
reader: DummyCompendiumDataSourceReader(items: [item]),
document: CompendiumSourceDocument.unknownCore,
document: CompendiumSourceDocument.unspecifiedCore,
overwriteExisting: false
)

Expand All @@ -52,7 +52,7 @@ class CompendiumImporterTest: XCTestCase {
sourceId: .defaultMonsters,
sourceVersion: nil,
reader: DummyCompendiumDataSourceReader(items: [item]),
document: CompendiumSourceDocument.unknownCore,
document: CompendiumSourceDocument.unspecifiedCore,
overwriteExisting: false
)

Expand All @@ -75,15 +75,23 @@ class CompendiumImporterTest: XCTestCase {
sourceId: .defaultMonsters,
sourceVersion: nil,
reader: DummyCompendiumDataSourceReader(items: [item]),
document: CompendiumSourceDocument.unknownCore,
overwriteExisting: true
document: CompendiumSourceDocument.unspecifiedCore,
overwriteExisting: false
)

_ = try await sut.run(task)

// change the item and import it again
item.stats.hitPoints = 1000
let result = try await sut.run(task)
let task2 = CompendiumImportTask(
sourceId: .defaultMonsters,
sourceVersion: nil,
reader: DummyCompendiumDataSourceReader(items: [item]),
document: CompendiumSourceDocument.unspecifiedCore,
overwriteExisting: true
)

let result = try await sut.run(task2)
XCTAssertEqual(result, CompendiumImporter.Result(newItemCount: 0, overwrittenItemCount: 1, invalidItemCount: 0))

let entry = try! compendium.database.keyValueStore.get(item.key)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6,026 changes: 2,438 additions & 3,588 deletions App/UnitTests/__Snapshots__/Open5eMonsterDataSourceReaderTest/test.1.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
- denominator: 1
- numenator: 13
▿ realm: core
- value: "core"
▿ value: core
- rawValue: "core"
▿ stats: StatBlock
▿ _alignment: Migrated<Optional<Alignment>, Optional<Alignment>>
▿ _alignment: Migrated<Optional<Alignment>>
▿ wrappedValue: Optional<Alignment>
▿ some: Alignment
▿ both: (2 elements)
Expand All @@ -32,48 +33,42 @@
▿ Parseable<CreatureAction, ParsedCreatureAction>
▿ input: CreatureAction
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-200000000000
- uuid: "00000000-0000-0000-0000-200000000000"
- wrappedValue: 00000000-0000-0000-0000-200000000000
- description: "The dragon can use its Frightful Presence. It then makes three attacks: one with its bite and two with its claws."
- name: "Multiattack"
- result: Optional<ParserResult>.none
▿ Parseable<CreatureAction, ParsedCreatureAction>
▿ input: CreatureAction
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-300000000000
- uuid: "00000000-0000-0000-0000-300000000000"
- wrappedValue: 00000000-0000-0000-0000-300000000000
- description: "Melee Weapon Attack: +11 to hit, reach 10 ft., one target. Hit: 17 (2d10 + 6) piercing damage plus 4 (1d8) cold damage."
- name: "Bite"
- result: Optional<ParserResult>.none
▿ Parseable<CreatureAction, ParsedCreatureAction>
▿ input: CreatureAction
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-400000000000
- uuid: "00000000-0000-0000-0000-400000000000"
- wrappedValue: 00000000-0000-0000-0000-400000000000
- description: "Melee Weapon Attack: +11 to hit, reach 5 ft., one target. Hit: 13 (2d6 + 6) slashing damage."
- name: "Claw"
- result: Optional<ParserResult>.none
▿ Parseable<CreatureAction, ParsedCreatureAction>
▿ input: CreatureAction
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-500000000000
- uuid: "00000000-0000-0000-0000-500000000000"
- wrappedValue: 00000000-0000-0000-0000-500000000000
- description: "Melee Weapon Attack: +11 to hit, reach 15 ft., one target. Hit: 15 (2d8 + 6) bludgeoning damage."
- name: "Tail"
- result: Optional<ParserResult>.none
▿ Parseable<CreatureAction, ParsedCreatureAction>
▿ input: CreatureAction
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-600000000000
- uuid: "00000000-0000-0000-0000-600000000000"
- wrappedValue: 00000000-0000-0000-0000-600000000000
- description: "Each creature of the dragon\'s choice that is within 120 ft. of the dragon and aware of it must succeed on a DC 14 Wisdom saving throw or become frightened for 1 minute. A creature can repeat the saving throw at the end of each of its turns, ending the effect on itself on a success. If a creature\'s saving throw is successful or the effect ends for it, the creature is immune to the dragon\'s Frightful Presence for the next 24 hours."
- name: "Frightful Presence"
- result: Optional<ParserResult>.none
▿ Parseable<CreatureAction, ParsedCreatureAction>
▿ input: CreatureAction
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-700000000000
- uuid: "00000000-0000-0000-0000-700000000000"
- wrappedValue: 00000000-0000-0000-0000-700000000000
- description: "The dragon exhales an icy blast in a 60-foot cone. Each creature in that area must make a DC 19 Constitution saving throw, taking 54 (12d8) cold damage on a failed save, or half as much damage on a successful one."
- name: "Cold Breath (Recharge 5-6)"
- result: Optional<ParserResult>.none
Expand Down Expand Up @@ -103,16 +98,14 @@
▿ Parseable<CreatureFeature, ParsedCreatureFeature>
▿ input: CreatureFeature
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-000000000000
- uuid: "00000000-0000-0000-0000-000000000000"
- wrappedValue: 00000000-0000-0000-0000-000000000000
- description: "The dragon can move across and climb icy surfaces without needing to make an ability check. Additionally, difficult terrain composed of ice or snow doesn\'t cost it extra moment."
- name: "Ice Walk"
- result: Optional<ParserResult>.none
▿ Parseable<CreatureFeature, ParsedCreatureFeature>
▿ input: CreatureFeature
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-100000000000
- uuid: "00000000-0000-0000-0000-100000000000"
- wrappedValue: 00000000-0000-0000-0000-100000000000
- description: "If the dragon fails a saving throw, it can choose to succeed instead."
- name: "Legendary Resistance (3/Day)"
- result: Optional<ParserResult>.none
Expand All @@ -139,24 +132,21 @@
▿ Parseable<CreatureAction, ParsedCreatureAction>
▿ input: CreatureAction
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-800000000000
- uuid: "00000000-0000-0000-0000-800000000000"
- wrappedValue: 00000000-0000-0000-0000-800000000000
- description: "The dragon makes a Wisdom (Perception) check."
- name: "Detect"
- result: Optional<ParserResult>.none
▿ Parseable<CreatureAction, ParsedCreatureAction>
▿ input: CreatureAction
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-900000000000
- uuid: "00000000-0000-0000-0000-900000000000"
- wrappedValue: 00000000-0000-0000-0000-900000000000
- description: "The dragon makes a tail attack."
- name: "Tail Attack"
- result: Optional<ParserResult>.none
▿ Parseable<CreatureAction, ParsedCreatureAction>
▿ input: CreatureAction
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-100000000000
- uuid: "00000000-0000-0000-0000-100000000000"
- wrappedValue: 00000000-0000-0000-0000-100000000000
- description: "The dragon beats its wings. Each creature within 10 ft. of the dragon must succeed on a DC 19 Dexterity saving throw or take 13 (2d6 + 6) bludgeoning damage and be knocked prone. The dragon can then fly up to half its flying speed."
- name: "Wing Attack (Costs 2 Actions)"
- result: Optional<ParserResult>.none
Expand Down Expand Up @@ -223,9 +213,10 @@
- denominator: 1
- numenator: 1
▿ realm: core
- value: "core"
▿ value: core
- rawValue: "core"
▿ stats: StatBlock
▿ _alignment: Migrated<Optional<Alignment>, Optional<Alignment>>
▿ _alignment: Migrated<Optional<Alignment>>
▿ wrappedValue: Optional<Alignment>
▿ some: Alignment
▿ both: (2 elements)
Expand All @@ -251,16 +242,14 @@
▿ Parseable<CreatureAction, ParsedCreatureAction>
▿ input: CreatureAction
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-130000000000
- uuid: "00000000-0000-0000-0000-130000000000"
- wrappedValue: 00000000-0000-0000-0000-130000000000
- description: "Melee Weapon Attack: +4 to hit, reach 5 ft., one target. Hit: 11 (2d8 + 2) piercing damage."
- name: "Morningstar"
- result: Optional<ParserResult>.none
▿ Parseable<CreatureAction, ParsedCreatureAction>
▿ input: CreatureAction
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-140000000000
- uuid: "00000000-0000-0000-0000-140000000000"
- wrappedValue: 00000000-0000-0000-0000-140000000000
- description: "Melee or Ranged Weapon Attack: +4 to hit, reach 5 ft. or range 30/120 ft., one target. Hit: 9 (2d6 + 2) piercing damage in melee or 5 (1d6 + 2) piercing damage at range."
- name: "Javelin"
- result: Optional<ParserResult>.none
Expand All @@ -279,16 +268,14 @@
▿ Parseable<CreatureFeature, ParsedCreatureFeature>
▿ input: CreatureFeature
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-110000000000
- uuid: "00000000-0000-0000-0000-110000000000"
- wrappedValue: 00000000-0000-0000-0000-110000000000
- description: "A melee weapon deals one extra die of its damage when the bugbear hits with it (included in the attack)."
- name: "Brute"
- result: Optional<ParserResult>.none
▿ Parseable<CreatureFeature, ParsedCreatureFeature>
▿ input: CreatureFeature
▿ _id: Wrapper<UUID>
▿ wrappedValue: 00000000-0000-0000-0000-120000000000
- uuid: "00000000-0000-0000-0000-120000000000"
- wrappedValue: 00000000-0000-0000-0000-120000000000
- description: "If the bugbear surprises a creature and hits it with an attack during the first round of combat, the target takes an extra 7 (2d6) damage from the attack."
- name: "Surprise Attack"
- result: Optional<ParserResult>.none
Expand Down Expand Up @@ -362,7 +349,8 @@
- name: "Aid"
- range: "30 feet"
▿ realm: core
- value: "core"
▿ value: core
- rawValue: "core"
- ritual: false
- school: "Abjuration"
▿ Spell
Expand All @@ -384,6 +372,7 @@
- name: "Acid Splash"
- range: "60 feet"
▿ realm: core
- value: "core"
▿ value: core
- rawValue: "core"
- ritual: false
- school: "Conjuration"
2 changes: 1 addition & 1 deletion Tests/MechMuseTests/CreatureActionDescriptionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class CreatureActionDescriptionTest: XCTestCase {
XCTAssertNoDifference(request.prompt(), [
.init(role: .system, content: "You are a Dungeons & Dragons DM."),
.init(role: .user, content: """
During a combat encounter, a player is attacked by a monster.Attacking monster: Goblin ().The monster attacks using "Shortbow".The attack is a hit, dealing 6 points of bludgeoning damage.Narrate the attack, focus on the monster and the attack, using a gritty style.Mention the damage.
During a combat encounter, a player is attacked by a monster.Attacking monster: Goblin ().The monster attacks using "Shortbow".The attack is a hit, dealing 6 points of bludgeoning damage.Narrate the attack to the player, focus on the monster and the attack, using a gritty style, limit to one paragraph.Mention the damage.
"""
)
])
Expand Down
8 changes: 5 additions & 3 deletions Tests/MechMuseTests/MechMuseTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,16 @@ final class MechMuseTest: XCTestCase {

extension OpenAIClient {
static func simpleMock(
performCompletionResponse: CompletionResponse?,
streamCompletionResponse: AsyncThrowingStream<String, Error>?,
streamChatResponse: AsyncThrowingStream<String, Error>?,
performCompletionResponse: CompletionResponse? = nil,
streamCompletionResponse: AsyncThrowingStream<String, Error>? = nil,
performChatResponse: ChatCompletionResponse? = nil,
streamChatResponse: AsyncThrowingStream<String, Error>? = nil,
modelsResponse: ModelsResponse = ModelsResponse()
) -> Self {
return OpenAIClient(
performCompletionRequest: { _ in performCompletionResponse! },
streamCompletionRequest: { _ in streamCompletionResponse! },
performChatCompletionRequest: { _ in performChatResponse! },
streamChatRequest: { _ in streamChatResponse! },
performModelsRequest: { modelsResponse }
)
Expand Down
25 changes: 19 additions & 6 deletions Tests/OpenAIClientTests/OpenAIClientTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ final class OpenAIClientTest: XCTestCase {
))

// Assert serialized request
let requestString = """
let requestData = """
{"model":"text-davinci-003","stream":false,"prompt":"Say this is a test"}
"""
XCTAssertNoDifference((httpClient.dataRequests.last?.httpBody).map { String(data: $0, encoding: .utf8)}, requestString)
""".data(using: .utf8)!
try XCTAssertNoDifferenceJSONData(requestData, httpClient.dataRequests.last?.httpBody)

// Assert parsed response
XCTAssertNoDifference(response, CompletionResponse(
Expand Down Expand Up @@ -77,10 +77,10 @@ final class OpenAIClientTest: XCTestCase {
))

// Assert serialized request
let requestString = """
let requestData = """
{"model":"gpt-3.5-turbo","stream":true,"messages":[{"content":"You are a D&D DM","role":"system"},{"content":"Narrate the attack of a goblin","role":"user"}]}
"""
XCTAssertNoDifference((httpClient.streamRequests.last?.httpBody).map { String(data: $0, encoding: .utf8)}, requestString)
""".data(using: .utf8)!
try XCTAssertNoDifferenceJSONData(requestData, httpClient.streamRequests.last?.httpBody)

// Assert parsed response
let string = try await response.reduce("", +)
Expand All @@ -107,4 +107,17 @@ final class OpenAIClientTest: XCTestCase {
}
}

func XCTAssertNoDifferenceJSONData(
_ expression1: @autoclosure () throws -> Data?,
_ expression2: @autoclosure () throws -> Data?,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) throws {
let json1 = try JSONSerialization.jsonObject(with: expression1() ?? Data()) as! AnyHashable
let json2 = try JSONSerialization.jsonObject(with: expression2() ?? Data()) as! AnyHashable

XCTAssertNoDifference(json1, json2, message(), file: file, line: line)
}

}

0 comments on commit 3cda9cb

Please sign in to comment.