From a064de5d5f51d39cf1bf633519dc7c2866b6b025 Mon Sep 17 00:00:00 2001
From: vmarcella <vmarcella@microsoft.com>
Date: Thu, 18 Jul 2024 15:24:43 -0700
Subject: [PATCH 1/2] [update] error message when unable to find the title,
 make the error a warning for scenario parsing, and update the scenario to use
 the file name as the scenario title.

---
 internal/engine/common/scenario.go      |  9 +++++++-
 internal/engine/common/scenario_test.go | 28 +++++++++++++++++++++++++
 internal/parsers/markdown.go            |  2 +-
 3 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/internal/engine/common/scenario.go b/internal/engine/common/scenario.go
index db01dfe9..c349756b 100644
--- a/internal/engine/common/scenario.go
+++ b/internal/engine/common/scenario.go
@@ -184,9 +184,16 @@ func CreateScenarioFromMarkdown(
 
 	// Group the code blocks into steps.
 	steps := groupCodeBlocksIntoSteps(codeBlocks)
+
+	// If no title is found, we simply use the name of the markdown file as
+	// the title of the scenario.
 	title, err := parsers.ExtractScenarioTitleFromAst(markdown, source)
 	if err != nil {
-		return nil, err
+		logging.GlobalLogger.Warnf(
+			"Failed to extract scenario title: '%s'. Using the name of the markdown as the scenario title",
+			err,
+		)
+		title = filepath.Base(path)
 	}
 
 	logging.GlobalLogger.Infof("Successfully built out the scenario: %s", title)
diff --git a/internal/engine/common/scenario_test.go b/internal/engine/common/scenario_test.go
index 8a06015f..1f162b4a 100644
--- a/internal/engine/common/scenario_test.go
+++ b/internal/engine/common/scenario_test.go
@@ -5,6 +5,7 @@ import (
 	"net/http"
 	"net/http/httptest"
 	"os"
+	"path/filepath"
 	"strings"
 	"testing"
 
@@ -79,6 +80,33 @@ func TestResolveMarkdownSource(t *testing.T) {
 	})
 }
 
+func TestScenarioParsing(t *testing.T) {
+	// Test parsing a scenario from markdown
+	t.Run("Parse scenario that doesn't have an h1 tag to use for it's title", func(t *testing.T) {
+		content := "Test content from local file"
+		temporaryFile, err := os.CreateTemp("", "example")
+		if err != nil {
+			t.Fatalf("Error creating temporary file: %v", err)
+		}
+		defer os.Remove(temporaryFile.Name())
+
+		if _, err := temporaryFile.Write([]byte(content)); err != nil {
+			t.Fatalf("Error writing to temporary file: %v", err)
+		}
+		if err := temporaryFile.Close(); err != nil {
+			t.Fatalf("Error closing temporary file: %v", err)
+		}
+
+		path := temporaryFile.Name()
+
+		scenario, err := CreateScenarioFromMarkdown(path, []string{"bash"}, nil)
+
+		assert.NoError(t, err)
+		fmt.Println(scenario)
+		assert.Equal(t, filepath.Base(path), scenario.Name)
+	})
+}
+
 func TestVariableOverrides(t *testing.T) {
 	variableScenarioPath := "../../../scenarios/testing/variables.md"
 	// Test overriding environment variables
diff --git a/internal/parsers/markdown.go b/internal/parsers/markdown.go
index f777a29b..f1ea85d1 100644
--- a/internal/parsers/markdown.go
+++ b/internal/parsers/markdown.go
@@ -75,7 +75,7 @@ func ExtractScenarioTitleFromAst(node ast.Node, source []byte) (string, error) {
 	})
 
 	if header == "" {
-		return "", fmt.Errorf("no header found")
+		return "", fmt.Errorf("no h1 header found to use as the scenario title")
 	}
 
 	return header, nil

From 422f8893673efbf8e415803e00fab07dba82788e Mon Sep 17 00:00:00 2001
From: vmarcella <vmarcella@microsoft.com>
Date: Thu, 18 Jul 2024 15:32:21 -0700
Subject: [PATCH 2/2] [add] test case for when the h1 tag is present.

---
 internal/engine/common/scenario_test.go | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/internal/engine/common/scenario_test.go b/internal/engine/common/scenario_test.go
index 1f162b4a..eb20768b 100644
--- a/internal/engine/common/scenario_test.go
+++ b/internal/engine/common/scenario_test.go
@@ -105,6 +105,31 @@ func TestScenarioParsing(t *testing.T) {
 		fmt.Println(scenario)
 		assert.Equal(t, filepath.Base(path), scenario.Name)
 	})
+
+	// Test parsing a scenario from markdown
+	t.Run("Parse scenario that does have an h1 tag to use for it's title", func(t *testing.T) {
+		content := "# Scenario-title\nTest content from local file"
+		temporaryFile, err := os.CreateTemp("", "example")
+		if err != nil {
+			t.Fatalf("Error creating temporary file: %v", err)
+		}
+		defer os.Remove(temporaryFile.Name())
+
+		if _, err := temporaryFile.Write([]byte(content)); err != nil {
+			t.Fatalf("Error writing to temporary file: %v", err)
+		}
+		if err := temporaryFile.Close(); err != nil {
+			t.Fatalf("Error closing temporary file: %v", err)
+		}
+
+		path := temporaryFile.Name()
+
+		scenario, err := CreateScenarioFromMarkdown(path, []string{"bash"}, nil)
+
+		assert.NoError(t, err)
+		fmt.Println(scenario)
+		assert.Equal(t, "Scenario-title", scenario.Name)
+	})
 }
 
 func TestVariableOverrides(t *testing.T) {