package conversion

import (
	"bytes"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"
)

const testAction = `SoundRequest sound_request
---
bool playing
time stamp
---
bool playing
time stamp
`

const testActionGo = `//autogenerated:yes
//nolint:revive,lll
package main

import (
    "github.com/bluenviron/goroslib/v2/pkg/msg"
    "time"
)


type MyactionActionGoal struct {
    msg.Package ` + "`" + `ros:"main"` + "`" + `
    SoundRequest SoundRequest
}



type MyactionActionResult struct {
    msg.Package ` + "`" + `ros:"main"` + "`" + `
    Playing bool
    Stamp time.Time
}



type MyactionActionFeedback struct {
    msg.Package ` + "`" + `ros:"main"` + "`" + `
    Playing bool
    Stamp time.Time
}

type myactionAction struct {
    msg.Package ` + "`" + `ros:"my_package"` + "`" + `
    myactionActionGoal
    myactionActionResult
    myactionActionFeedback
}
`

func TestImportAction(t *testing.T) {
	dir, err := os.MkdirTemp("", "goroslib")
	require.NoError(t, err)
	defer os.RemoveAll(dir)

	fpath := filepath.Join(dir, "myaction.action")
	err = os.WriteFile(fpath, []byte(testAction), 0o644)
	require.NoError(t, err)

	var buf bytes.Buffer
	err = ImportAction(fpath, "main", "my_package", &buf)
	require.NoError(t, err)
	require.Equal(t, testActionGo, buf.String())
}
