Repository: dotnet/runtime
File: src/mono/wasm/Wasm.Build.Tests/Blazor/EventPipeDiagnosticsTests.cs (C#)

Code before:
            
            const traces = await ${traceCommand};

            console.log(`Tracing done ${new Date().toISOString()}`);
                    
            // concatenate the buffers into a single Uint8Array
            const concatenated = new Uint8Array(traces.reduce((acc, curr) => acc + curr.byteLength, 0));
            let offset = 0;
            for (const trace of traces) {
                concatenated.set(new Uint8Array(trace), offset);
                offset += trace.byteLength;
            }

            console.log(`File ${filename} size to upload: ${concatenated.byteLength} bytes ${new Date().toISOString()}`);
            await fetch(`/upload/${filename}`, {
                headers: {
                    'Content-Type': 'application/octet-stream'
                },
                method: 'POST',
                body: concatenated
            });
            console.log(`File uploaded successfully: ${new Date().toISOString()}`);
            await new Promise(resolve => setTimeout(resolve, 1000));
            console.log(`Shutting down: ${new Date().toISOString()}`);
            console.log(`WASM EXIT 0`);
        };
        console.log(`globalThis.collectAndUpload method created ${new Date().toISOString()}`);
        """;
        up = up.Replace("${traceCommand}", traceCommand).Replace("${filename}", fileName);
        await page.EvaluateAsync(up);
    }

    private async Task ClickAndCollect(IPage page)
    {
        await page.EvaluateAsync(@"globalThis.donePromise = globalThis.collectAndUpload();");
        _testOutput.WriteLine($"Installed script: {DateTime.Now.ToString("O")}");

        // Click the button a few times
        for (int i = 0; i < 5; i++)
        {
            await page.Locator("text=\"Click me\"").ClickAsync();
            await Task.Delay(10);
        }
        _testOutput.WriteLine($"Done clicking: {DateTime.Now.ToString("O")}");

        var txt2 = await page.Locator("p[role='status']").InnerHTMLAsync();
        Assert.NotEqual("Current count: 0", txt2);
    }
}


Review comment (suggestion):
The comment refers to "uploadTrace" but the method is now named "collectAndUpload". This should be updated to match the actual method name.

Code after:
            const traces = await ${traceCommand};

            console.log(`Tracing done ${new Date().toISOString()}`);
                    
            // concatenate the buffers into a single Uint8Array
            const concatenated = new Uint8Array(traces.reduce((acc, curr) => acc + curr.byteLength, 0));
            let offset = 0;
            for (const trace of traces) {
                concatenated.set(new Uint8Array(trace), offset);
                offset += trace.byteLength;
            }

            console.log(`File ${filename} size to upload: ${concatenated.byteLength} bytes ${new Date().toISOString()}`);
            await fetch(`/upload/${filename}`, {
                headers: {
                    'Content-Type': 'application/octet-stream'
                },
                method: 'POST',
                body: concatenated
            });
            console.log(`File uploaded successfully: ${new Date().toISOString()}`);
            await new Promise(resolve => setTimeout(resolve, 1000));
            console.log(`Shutting down: ${new Date().toISOString()}`);
            console.log(`WASM EXIT 0`);
        };
        console.log(`globalThis.collectAndUpload method created ${new Date().toISOString()}`);
        """;
        up = up.Replace("${traceCommand}", traceCommand).Replace("${filename}", fileName);
        await page.EvaluateAsync(up);
    }

    private async Task ClickAndCollect(IPage page)
    {
        // Use void to prevent Playwright from awaiting the returned Promise,
        // so tracing runs in parallel with button clicks below.
        await page.EvaluateAsync(@"void (globalThis.donePromise = globalThis.collectAndUpload())");
        _testOutput.WriteLine($"Installed script: {DateTime.Now.ToString("O")}");

        // Click the button a few times while tracing is running
        for (int i = 0; i < 5; i++)
        {
            await page.Locator("text=\"Click me\"").ClickAsync();
            await Task.Delay(10);
        }
        _testOutput.WriteLine($"Done clicking: {DateTime.Now.ToString("O")}");

        var txt2 = await page.Locator("p[role='status']").InnerHTMLAsync();
        Assert.NotEqual("Current count: 0", txt2);

        // Wait for trace collection and upload to complete
        await page.EvaluateAsync(@"globalThis.donePromise");