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
1 change: 1 addition & 0 deletions .fantomasignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FableEntry.g.fs
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,25 @@ jobs:

fable:
runs-on: ubuntu-latest
# The version the library is packed as and the tests compile against
env:
Flip7Package: 0.0.0-fable
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6
with:
dotnet-version: '10.0.x'
- run: dotnet tool restore
- run: dotnet fable src/Flip7.fsproj --outDir ${{ runner.temp }}/fable
- run: dotnet pack src/Flip7.fsproj -c Release -o ${{ runner.temp }}/feed -p:Version=${{ env.Flip7Package }}
# The tests compile against the packed library, as a Fable project that
# depends on it would, and run on the runner's own Node
- name: Compile the tests to JavaScript
env:
RestoreAdditionalProjectSources: ${{ runner.temp }}/feed
run: |
dotnet fsi test/FableEntry.fsx
dotnet fable test/Flip7.Test.fsproj --outDir ${{ runner.temp }}/fable-test
echo '{ "type": "module" }' > ${{ runner.temp }}/fable-test/package.json
- name: Run the tests on Node
run: node ${{ runner.temp }}/fable-test/FableEntry.g.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ output/
artifacts/

timelines/
FableEntry.g.fs
2 changes: 1 addition & 1 deletion src/Flip7.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<IsPackable>true</IsPackable>
<PackageId>Flip7</PackageId>
<Description>The card game Flip 7 in F#: a rules engine that plays whole games, the odds a hand busts and what hitting it is worth, an exact search of the decision, strategies to play against, and inference of the strategy a player is using.</Description>
<PackageTags>fsharp;flip7;card-game;simulation;probability</PackageTags>
<PackageTags>fsharp;flip7;card-game;simulation;probability;fable;fable-library;fable-javascript;fable-dotnet</PackageTags>
<!-- The package depends on the FSharp.Core it was built against, so build against the first
one F# 10 shipped rather than this SDK's, which would force consumers onto it too. The
terminal app and the tests keep the SDK's. -->
Expand Down
4 changes: 3 additions & 1 deletion test/AimTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ let private roadsOf (timeline: Instant list) (asked: Asked list) : Roads =
)
}

#if !FABLE_COMPILER
[<Fact>]
let ``the questions a timeline reveals are the questions the engine asked, one for one`` () =
let roads = ResizeArray<Roads>()
Expand Down Expand Up @@ -341,6 +342,7 @@ let ``the aims follow a deal3 down its rarest roads`` () =
(roads 1266).AimsPassedTwice > 0,
"seed 1266 no longer passes two second chances on during one deal3's flips; pick a new seed"
)
#endif

// ---- Inference: how a player aims ----

Expand Down Expand Up @@ -368,7 +370,7 @@ let private pickedBy (targeting: Targeting) (aim: Observation) : string =
let private genAims: Gen<Observation list> =
genGameWithFork
|> Gen.map (fun (seed, lineup, _) ->
let timeline, _, _ = playForked id seed lineup None
let timeline = Timeline.SimulateWithDecider (Random seed) own lineup |> played

observationsOf timeline
|> List.where (fun observation -> observation.Choice.IsChoice2Of2)
Expand Down
68 changes: 66 additions & 2 deletions test/DeckTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ let ``an empty deck takes the discards in as the new deck`` () =
Assert.True(Deck.IsEmpty discards')
Assert.Equal(91I, Deck.Count deck')

#if !FABLE_COMPILER
[<Fact>]
let ``drawing with every card in a hand fails rather than hangs`` () =
// Two empty piles used to be shuffled into each other forever, so the draw
Expand All @@ -87,6 +88,7 @@ let ``drawing with every card in a hand fails rather than hangs`` () =
)

Assert.Contains("the deck and the discards are both empty", error.Message)
#endif

// The deck and the discards pooled into one count per card
let private pooled (deck: Deck, discards: Deck) : Deck =
Expand Down Expand Up @@ -120,6 +122,60 @@ let ``a draw conserves every card and touches the discards only to reshuffle the
)
|> Check.QuickThrowOnFailure

// A seed deals what it always has, since the digests of whole games, the seeds
// other tests were found at and the --seed of the terminal app all rest on it.
// A draw takes the card FSharp.Core's Array.randomShuffleWith puts first among
// the deck's cards laid out in Map order, and makes the calls that shuffle
// makes: Next(0, n), which picks the card, then Next(i, n) for each i from 1
// to n - 2. Fable's shuffle makes the same calls since Fable 5.27.0, but the
// calls are logged with a Recording, so the test runs on .NET only
#if !FABLE_COMPILER
[<Fact>]
let ``a draw takes the card FSharp.Core's shuffle puts first, and costs the Random what the shuffle does`` () =
// A lone card costs no call and two cards only the one that picks, so both
// are drawn beside FsCheck's decks
let decks =
Gen.frequency [
1, Gen.map (List.singleton >> deckOf) genCard
1, Gen.map deckOf (Gen.listOfLength 2 genCard)
4, Gen.filter (Deck.IsEmpty >> not) genDeck
]

Prop.forAll
(Arb.fromGen (Gen.zip genSeed decks))
(fun (seed, deck) ->
let cards =
deck
|> Map.toArray
|> Array.collect (fun (card, count) -> Array.replicate (int count) card)

let shuffled = Random seed
let first = Array.randomShuffleWith shuffled cards |> Array.head

let recording = Recording seed
let _, drawn = Deck.Draw1With recording (deck, Deck.Empty)
let made = List.ofSeq recording.Made
let leftAsShuffled = recording.Next() = shuffled.Next()

// The calls a Random of the same seed answers the same way
let replay = Random seed
let n = cards.Length
let expected = [ for i in 0 .. n - 2 -> Shuffled(0, i, n, replay.Next(i, n)) ]

let picked =
match expected with
| Shuffled(_, _, _, index) :: _ -> cards[index]
| _ -> cards[0]

Prop.label "the draw calls Next(i, n) for each i from 0 to n - 2 and nothing else" (made = expected)
.&. Prop.label "the card is the one the first call picks, counting copies in Map order" (drawn = picked)
.&. Prop.label
"the card is the one the shuffle puts first, and the Random is left where the shuffle leaves it"
(drawn = first && leftAsShuffled)
)
|> Check.QuickThrowOnFailure
#endif

// ---- saved format ----

[<Fact>]
Expand Down Expand Up @@ -173,14 +229,22 @@ let ``a deck that is not exactly the lines Serialize writes is None rather than
]

// An exception is reported under its label like a deck that read, so one
// bad input that raises does not hide the rest
// bad input that raises does not hide the rest: by its type, or in
// JavaScript, where Fable cannot name it, by its message
let raised (error: exn) =
#if FABLE_COMPILER
error.Message
#else
error.GetType().Name
#endif

let wrong = [
for label, lines in badInputs do
try
if Option.isSome (Deck.TryDeserialize lines) then
yield $"{label}: read as a deck"
with error ->
yield $"{label}: raised {error.GetType().Name}"
yield $"{label}: raised {raised error}"
]

Assert.True(wrong.IsEmpty, String.concat "\n" wrong)
Expand Down
Loading
Loading