// Copyright (c) 2025 H0llyW00dzZ All rights reserved. // // By accessing or using this software, you agree to be bound by the terms // of the License Agreement, which you can find at LICENSE files. package export_test import ( "testing" "git.b0zal.io/H0llyW00dzZ/imap/export" "github.com/stretchr/testify/assert" "github.com/valyala/bytebufferpool" ) func TestJSONExporter(t *testing.T) { // Create a sample message messages := []map[string]any{ { "id": "1", "from": []string{"contact@example.org"}, "subject": "A little message, just for you", "body": "Hi there :)", }, } // Get a buffer from the pool buffer := bytebufferpool.Get() defer func() { buffer.Reset() // Reset the buffer to prevent data leaks bytebufferpool.Put(buffer) // Return the buffer to the pool }() // Create a JSONExporter with the default encoder exporter := &export.JSONExporter{ Encoder: export.DefaultJSONEncoder, } // Export the messages to the buffer err := exporter.Export(messages, buffer) assert.NoError(t, err, "Failed to export messages") // Define the expected JSON output expectedJSON := `[{"body":"Hi there :)","from":["contact@example.org"],"id":"1","subject":"A little message, just for you"}] ` // Assert that the buffer content matches the expected JSON assert.Equal(t, expectedJSON, buffer.String(), "Unexpected JSON output") }