screepers / screeps-server-mockup

Private server package for unit tests
MIT License
55 stars 13 forks source link

Easily adding objects/structures to test world #6

Open brisberg opened 4 years ago

brisberg commented 4 years ago

First of all, thanks for taking over this project and moving it to the Screepers group. I was bummed that Integration testing didn't work out of the box a few months ago, so I am happy to have it back!

Anyways, my question is asking if there is an easy, out-of-the-box way to add Game Objects and Structures to the test world to set up realistic, repeatable scenarios.

From the samples, you can add a structure (a Spawn in this case) by directly executing a database update:

db['rooms.objects'].insert({
  room, type: 'spawn', x, y, user: user._id, name: spawnName,
  store : { energy: C.SPAWN_ENERGY_START },
  storeCapacityResource: { energy: C.SPAWN_ENERGY_CAPACITY },
  hits: C.SPAWN_HITS, hitsMax: C.SPAWN_HITS, spawning: null, notifyWhenAttacked: true
})

Not all of these fields are technically required (simply adding type, room, x,y, and user will add a Spawn Structure with no storage), but all of these fields are required to create a Spawn structure that behaves exactly like a Spawn Structure on live.


Is there a set of convenience methods on any of the Screeps projects to add structures/creeps as they exist on live?

If not, do you think this project would be a reasonable place to put such wrappers? That way they could be updated whenever the Screeps version is updated.

I am willing to put up a PR with these methods, if you guys think this would be a good place for them.

Hiryus commented 4 years ago

When I wrote it, I was thinking about placing these kind of methods in src/world.js.

As a start, you have addRoomObject() which is quite generic but still more abstract than manipulating database directly. You can see some examples in world.tests.js but I don't think I ever used it for owned creeps or structures.

If needed, it should be fairly easy (but quite time consuming) to implement more specific methods on top of it. The file grew up quite fast though, it may be better to split these functions into another one.

I leave it to @pyrodogg for final decision.

brisberg commented 4 years ago

Thanks for the reply, I agree addRoomObject() is the better base abstraction on which to start.

I don't mind implementing the specific methods. As it is I am already slated to write them for my own project because I feel it is necessary to write readable tests. Adding them to src/world.js or src/roomObjects.js etc sounds fine.

Just let me know your or @pyrodogg's preference.

pyrodogg commented 4 years ago

Its been on my mind to add more convenience functions for mocking room scenarios. Also, documentation and examples for how to use them.

Putting it in something like src/roomObject.js sounds good to start. If needed later, it could be broken up in even more detail.

brisberg commented 4 years ago

Sounds good! I'll give it a try this weekend and open a PR with a first attempt.

eduter commented 4 years ago

I don't mind implementing the specific methods. As it is I am already slated to write them for my own project because I feel it is necessary to write readable tests.

@brisberg if you manage to write readable tests using the mock server, I think it would be great if you published some examples of that, like I did with unit tests, using Jest. I can't really picture how these tests would look like and what's the benefit of using them, instead of unit tests. I'd love to see some concrete examples.

brisberg commented 4 years ago

@eduter Good idea, I'll definitely publish what I come up with. Just looked over your jest integration and it looks really good! I'll definitely make use of it for my unit testing.

I am hoping that these tests will be broad enough that they give confidence that your AI can accomplish a very broad task. I am imagining things like: "Given an operating RCL4 Colony, Harvest energy from a remote room", "Given a storage full of energy, a set of creeps, and a handful of construction sites, Build all of the sites", "Given a spawn/extension set, a Creep, and a Storage full of energy, Fill all of the Spawns/Extensions".

Hopefully these tests are mostly AI independent, so that you do not need to update them often, even with drastic changes to the AI. You can also set performance limits (CPU usage, Energy harvested / tick, number of ticks to distribute, etc) and ensure that your changes do not reduce efficiency in other aspects.

Also Combat Scenarios would be very useful. See if your squads of units can defeat other squads of units, base can defend a drain attack, etc. These kinds of multi-tick engagements would be very difficult to Unit test effectively.