Repository: microsoft/onefuzz
File: src/ApiService/IntegrationTests/AgentEventsTests.cs (C#)

Code before:
            Event: new NodeStateUpdate(NodeState.Ready));

        var result = await func.Run(TestHttpRequestData.FromJson("POST", data));
        Assert.Equal(HttpStatusCode.OK, result.StatusCode);

        var node = await Context.NodeOperations.SearchAll().SingleAsync();
        Assert.Equal(NodeState.Ready, node.State);
    }

    [Fact]
    public async Async.Task NodeStateUpdate_BecomingFree_StopsNode_IfMarkedForReimage() {
        await Context.InsertAll(
            new Node(_poolName, _machineId, _poolId, _poolVersion, ReimageRequested: true));

        var auth = new TestEndpointAuthorization(RequestType.Agent, Logger, Context);
        var func = new AgentEvents(Logger, auth, Context);
        var data = new NodeStateEnvelope(
            MachineId: _machineId,
            Event: new NodeStateUpdate(NodeState.Free));

        var result = await func.Run(TestHttpRequestData.FromJson("POST", data));
        Assert.Equal(HttpStatusCode.OK, result.StatusCode);

        await Async.Task.WhenAll(
            Async.Task.Run(async () => {
                // should still be in init state:
                var node = await Context.NodeOperations.SearchAll().SingleAsync();
                Assert.Equal(NodeState.New, node.State);
            }),
            Async.Task.Run(async () => {
                // the node should be told to stop:
                var messages = await Context.NodeMessageOperations.SearchAll().ToListAsync();
                Assert.Contains(messages, msg =>
                    msg.MachineId == _machineId &&
                    msg.Message.Stop == new StopNodeCommand());
            }));
    }

    [Fact]
    public async Async.Task NodeStateUpdate_BecomingFree_StopsNode_IfMarkedForDeletion() {
        await Context.InsertAll(
            new Node(_poolName, _machineId, _poolId, _poolVersion, DeleteRequested: true));

        var auth = new TestEndpointAuthorization(RequestType.Agent, Logger, Context);
        var func = new AgentEvents(Logger, auth, Context);
        var data = new NodeStateEnvelope(
            MachineId: _machineId,
            Event: new NodeStateUpdate(NodeState.Free));

        var result = await func.Run(TestHttpRequestData.FromJson("POST", data));
        Assert.Equal(HttpStatusCode.OK, result.StatusCode);

Review comment (suggestion):
NIT:
```suggestion
                // should still be in new state:
```

Code after:
            Event: new NodeStateUpdate(NodeState.Ready));

        var result = await func.Run(TestHttpRequestData.FromJson("POST", data));
        Assert.Equal(HttpStatusCode.OK, result.StatusCode);

        var node = await Context.NodeOperations.SearchAll().SingleAsync();
        Assert.Equal(NodeState.Ready, node.State);
    }

    [Fact]
    public async Async.Task NodeStateUpdate_BecomingFree_StopsNode_IfMarkedForReimage() {
        await Context.InsertAll(
            new Node(_poolName, _machineId, _poolId, _poolVersion, ReimageRequested: true));

        var auth = new TestEndpointAuthorization(RequestType.Agent, Logger, Context);
        var func = new AgentEvents(Logger, auth, Context);
        var data = new NodeStateEnvelope(
            MachineId: _machineId,
            Event: new NodeStateUpdate(NodeState.Free));

        var result = await func.Run(TestHttpRequestData.FromJson("POST", data));
        Assert.Equal(HttpStatusCode.OK, result.StatusCode);

        await Async.Task.WhenAll(
            Async.Task.Run(async () => {
                // should still be in init state:
                var node = await Context.NodeOperations.SearchAll().SingleAsync();
                Assert.Equal(NodeState.Init, node.State);
            }),
            Async.Task.Run(async () => {
                // the node should be told to stop:
                var messages = await Context.NodeMessageOperations.SearchAll().ToListAsync();
                Assert.Contains(messages, msg =>
                    msg.MachineId == _machineId &&
                    msg.Message.Stop == new StopNodeCommand());
            }));
    }

    [Fact]
    public async Async.Task NodeStateUpdate_BecomingFree_StopsNode_IfMarkedForDeletion() {
        await Context.InsertAll(
            new Node(_poolName, _machineId, _poolId, _poolVersion, DeleteRequested: true));

        var auth = new TestEndpointAuthorization(RequestType.Agent, Logger, Context);
        var func = new AgentEvents(Logger, auth, Context);
        var data = new NodeStateEnvelope(
            MachineId: _machineId,
            Event: new NodeStateUpdate(NodeState.Free));

        var result = await func.Run(TestHttpRequestData.FromJson("POST", data));
        Assert.Equal(HttpStatusCode.OK, result.StatusCode);