exaV / screeps-kotlin-types

Screep's Kotlin type definitions
MIT License
18 stars 18 forks source link

Tests cannot use anything defined as external #47

Open thigg opened 5 years ago

thigg commented 5 years ago

For an example, check my fork https://github.com/thigg/screeps-kotlin-types/commit/7a0dd6bf7ec3616adef2a43104f405ea07e4ce2e

This simple test fails:

assertEquals(126,FIND_MY_STRUCTURES.value or FIND_CREEPS.value)

with ReferenceError: FIND_MY_STRUCTURES is not defined

My guess is, that is because the constants are defined as external. I think that should either be documented as an issue or be fixed. Maybe we can define a npm dep for test-runtime which fixes that?

thigg commented 5 years ago

Typescrpt just redefines them: https://github.com/screepers/typed-screeps/blob/master/src/constants.ts

thigg commented 5 years ago

I tried to convert https://github.com/screepers/typed-screeps with https://github.com/Kotlin/ts2kt. (rename .ts file to d.ts files first) and got that as result. At least it has the values as comments now ;) Anyway thats no solution yet.

thigg commented 5 years ago

I tried creaiing a few values in the Constants as non external and put a copy with that into the test classpaths, thus overriding the original Constants.kt during tests.

That works for now, but we would have, at least for constants, 2 files, one externally defined and one local.

One question is, if the external definition has advantages for the constants

exaV commented 5 years ago

We decided leave external declarations as = definedExternallyand we stick to the pattern throughout the codebase. This is not only true for constants but also for default arguments and default values. Changing it would be error prone and would put the burden of maintaining the mapping on us, which I would like to avoid.

You should be able to run the tests by declaring a npm dependency on the actual screeps api where the constants are defined. I'm not sure if there's anything else you'd have to do for the values to be available at runtime. Let me know if you need help. Also let us know if you figure it out :)

thigg commented 5 years ago

I got something working @ https://github.com/thigg/screeps-kotlin-starter/blob/master/src/test/kotlin/SpawnTests.kt

This is a pretty ugly fix for now. also check the build file

blattlaus87 commented 4 years ago

I also tried to get something working but I stopped it since it is a huge effort to get something mocked and still existing game logic. My approach was to copy the constants into the compiled js code. This is still ugly but worked for some basic things. But when it comes to logic tests with Game/Memory/... you are quiet bussy in mocking things...

I also thougth about proper testing. The most bugs came from edge cases which I never thought about. So I also would not write a test beforehand. Meaning the tests would only have a benefit on refactorings and checking simple logic.

The advanced testing approach would be to setup a testing server/simulator (not in kotlin). Defining different test rooms and run the simulation for some ticks. And then monitor the rooms and check for energy level, filled extensions, blocked/number of creeps, other special cases you want to check. I think this is the only reliable way to avoid crashing your colony when making changes. Addionally you can also check some metrics about "time to claim/build up a room" / "defending time against creeps". But I had no time to setup the stuff and observing the bugs still works relatively good.

exaV commented 4 years ago

I remember doing some experiments when this issue first came up. I tried adding the engine dependencies to my test setup, which IRC solved the constants issues but other issues came up left and right so I gave up.

If I were to do it again I would try to setup a screeps engine for the tests, so that external declarations resolve correctly. It would also allow us to run some integration tests over multiple ticks. I would look into test container libraries on js that integrate well with one of the test frameworks we can use on kotlin-js. Then I would configure the tests to start a screeps-server container and run the tests against that.