package vo

import (
	"testing"

	"github.com/stretchr/testify/require"
	"github.com/tidbcloud/serverless-service/internal/domain/constant"
)

func TestPrettyMessage(t *testing.T) {
	msg := `[types:1292]Truncated incorrect DOUBLE value: ''
failed to cast value as bigint(20) for column ward (#13)
one
two
github.com/pingcap/tidb-lightning/lightning/mydump.(*mdLoaderSetup).setup
        /go/src/github.com/pingcap/tidb-lightning/lightning/mydum
`

	get := PrettyMessage(msg, constant.ImportDataFormatCsv)
	require.Equal(
		t,
		"CastValueError: [types:1292]Truncated incorrect DOUBLE value: ''. failed to cast value as bigint(20) for column ward (#13). one. two Please correct the source data and retry.",
		get,
	)

	msg = ""
	get = PrettyMessage(msg, constant.ImportDataFormatCsv)
	require.Empty(t, get)
}

func TestSimplifyError(t *testing.T) {
	errs := []string{
		"AccessDenied: The ciphertext refers to a customer master key that does not exist, does not exist in this region, or you are not allowed to access",
		"BucketRegionError: incorrect region, the bucket is not in 'ap-northeast-1' region at endpoint ''",
		"[Lightning:PreCheck:ErrPreCheckFailed]tidb-lightning pre-check failed: Region distribution is unbalanced, the ratio of the regions count of the store(8) with least regions(7) to the store(1) with most regions(95879) is 7.300868803387604e-05, but we expect it must not be less than 0.5",
		"[Lightning:PreCheck:ErrPreCheckFailed]tidb-lightning pre-check failed: table(s) [`gharchive_dev`.`github_events`] are not empty",
		"[Lightning:Restore:ErrSchemaNotExists]table `nft`.`nft` schema not found",
		"[Lightning:Restore:ErrEncodeKV]encode kv error in file NFT_Sales_History_2.csv:0 at offset 4: syntax error: cannot have consecutive fields without separator",
		"[Lightning:Restore:ErrUnknownColumns]unknown columns in header (id,name,volume,volume_usd,market_cap) for table nftHisto",
		"[Lightning:PreCheck:ErrCheckDataSource]check data source error: [Lightning:Restore:ErrUnknownColumns]unknown columns in header (c_95747439,aaa_3,1657221693126,q25jdq8dl5upmih2se7zw4dbctpr6uosxmt9hx8bchwuetmzy0fqscozruyfwanxlblqctsbq0xyp942gnt9wu2aawv56hxvgdifam4tlisqj0kw0kyd7j4pjvbsxfy64ipkmgfbokne120lwksrvjndj71g6wrhmtpjlgubh3q7sfxbr7htipe5j36kjwcp4iyezedcv5rxkwem0gpfzl0yjkvvafykixbyfa5aem6vr4qrbqjqq5ngyd7guthqycw5njilhtelrzvuw7o1kif9w6lojyv2u3erfar66zbxyvo8cmancnmn6hzg8xz1fqbgyjzqj9y2boliwfvkrpaloegy9qezslwnrgs3fpjnevcdxrwvpir7gqfvuibwwsk12nw0on61xe3vbwfg38zmamdkgy87bdycbrufraffoz4zk579icpq5f9rqlntdhuqfwwiam5pztrpabywdzzvhdig4npktbdmpbuyszmzhx0rqabst9ovffk3nuvr9wmd3zpeoj59tiz6qmzc0ejrqvuqqfelilokpsmzompeeowviixfryrrmoucilobtij8vxc6lz1eefhxowfwlc58dwkfrnx3ekxwxlececebgqryplem1pouj9gdfa95gpdq85m5mev8tat17ebfru628bh3e6e02ht4g6nnb8zxwvluccfawntu3kmwrsim96dbq7cawtdbzcdyzjysp8tsmm4fpkiv4bhushgcokh2btvferonl7rprqebtcraxrgjlngt5o0ptutwd8m41bh15fphpb9jemoemaanxzcattvfhnneqwdzx1c31nurcyisexsljzylcrqrkgm7uok4wkxvsa3hzz3jxkpmapj6sbwebwphh46t7jbzsxl1vb7vxqgmqz9phsvu30ch460vcdybnvkjmsrjgefloxmuclgd4f2ggyaskxaw1seezbman62vrfgdbifcjp) for table t",
		"checksum mismatched remote vs local => (checksum: 11392935869726273266 vs 16511259432307356781) (total_kvs: 2227130352 vs 2286517030) (total_bytes:169255050357 vs 190427391764)",
		"data-source-dir 's3://op-data-datastore-poc-prod/csv_latest_versions_20220222_uncompressed/?region=eu-central-1' doesn't exist or contains no files",
		"[Lightning:Config:ErrInvalidConfig]parse filter failed: at <cmdline>:1: missing table pattern",
		"[tikv:9002]TiKV server timeout",
		"cluster is deleted",
		"[Lightning:Common:ErrUnknown]unknown error: SharedConfigLoadError: failed to load config file, /.aws/config",
		"[Lightning:Storage:ErrEmptySourceDir]data-source-dir 's3://tidbcloud-samples-testimport/test_import/test?region=us-west-2' doesn't exist or contains no files",
		"[Lightning:Storage:ErrStorageUnknown]unknown storage error: AccessDenied: User: arn:aws:sts::385595570414:assumed-role/dbaas-shared-a225bc83-import/1656408111895559373 is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::385595570414:rol",
		"[Lightning:Config:ErrInvalidConfig]parse filter failed: at <cmdline>:1: missing table pattern",
		"[Lightning:Restore:ErrSchemaNotExists]table `db`.`test1` schema not found",
		"[Lighting:Restore:ErrChecksumMismatch]checksum mismatched remote vs local => (checksum: 13249482660652401355 vs 5396461279094460892) (total_kvs: 2 vs 4) (total_bytes:66 vs 132)",
		"[Lightning:Restore:ErrCreateSchema]create schema failed, table: `matrix`.`alerts_labels`, stmt: restore table schema: run create schema job failed: Error 1426: Too big precision 9 specified for column 'TIMESTAMP'. Maximum is 6.",
		"[Lightning:PreCheck:ErrCheckCSVHeader]check csv header error: syntax error: cannot have consecutive fields without separator",
		"[Lightning:PreCheck:ErrCheckDataSource]check data source error: source csv files contains header row but mydumper.csv.header is false",
		"[Lightning:Restore:ErrRestoreTable]restore table edw_bin.transactions failed: The available disk of TiKV (db-tikv-0.db-tikv-peer.tidb1379661944644004109.svc:20160) only left 435939180544, and capacity is 4362862139015",
		"[tikv:9005]Region is unavailable",
		"[Lightning:Restore:ErrEncodeKV]encode kv error in file tpcc.new_order.000000000.csv:0 at offset 7536640: read tcp 10.0.160.67:37728->52.92.225.184:443: read: connection reset by peer",
		"[Lightning:Restore:ErrRestoreTable]restore table sf100_starschema.lineorder failed: failed to send request to remote worker, http://load-data-worker-1705686528495372542-104--1-service.tidb-serverless.svc.cluster.local:19500/load_data?cluster_id=7158108932969803892&task_id=1705686528495372542-104--1, status 404 Not Found",
		"[Lightning:Restore:ErrRestoreTable]restore table `gharchive_dev`.`github_events` failed: chunk-26 not found",
		"[Lightning:Restore:ErrEncodeKV]encode kv error in file test.llama_index_rag_test.csv:0 at offset 25317: failed to cast value as varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin for column `document` (#3): [types:1406]Data Too Long, field len 512, data len 4148",
		"[Lightning:PreCheck:ErrPreCheckFailed]tidb-lightning pre-check failed: TiDB schema `constructai`.`arbetsmoment` doesn't have column arbetsmoment, please check table schema and csv file header or use tables.ignoreColumns to ignore arbetsmoment\nTiDB schema `constructai`.`arbetsmoment` doesn't have the default value for arbetsmoment_name. Please add default value for column 'arbetsmoment_name' or choose another column to ignore or add this column in data file",
		"[Lightning:Common:ErrUnknown]unknown error: GET https://tidbserverless.blob.core.windows.net/serverless-e2e-import\n--------------------------------------------------------------------------------\nRESPONSE 403: 403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nERROR CODE: AuthenticationFailed\n--------------------------------------------------------------------------------\n<?xml version=\"1.0\" encoding=\"utf-8\"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:ddb5fa40-b01e-00da-4e5d-787c9b000000\nTime:2025-02-06T06:09:54.1031497Z</Message><AuthenticationErrorDetail>The specified signed resource is not allowed for the this resource level</AuthenticationErrorDetail></Error>\n--------------------------------------------------------------------------------\n",
		"[Lightning:Storage:ErrStorageUnknown]unknown storage error: googleapi: Error 403: test-import-no-privilege@gcp-serverless-ecosystem-dev.iam.gserviceaccount.com does not have storage.objects.list access to the Google Cloud Storage bucket. Permission 'storage.objects.list' denied on resource (or it may not exist)., forbidden",
		"[Lightning:Restore:ErrRestoreTable]restore table `test`.`a` failed: region has overlap data",
		"[Lightning:Restore:ErrRestoreTable]restore table `test`.`i` failed: too many duplicate key",
		"[types:1292]Truncated incorrect DOUBLE value: ''\nfailed to cast value as bigint(20) for column `ward` (#13)",
		"[Lightning:Restore:ErrEncodeKV]encode kv error in file test.a.csv:0 at offset 15: failed to cast value as int(11) for column `name` (#1): [types:1292]Truncated incorrect DOUBLE value: 'a'",
		"[Import:ErrCastValue]Value conversion failed for column 'c1'. Expected type: tinyint(4), received value: 10000000. Reason: [types:1690]constant 10000000 overflows tinyint.",
		"[Lightning:Restore:ErrEncodeKV]encode kv error in file test.a.csv:0 at offset 15: [Import:ErrCastValue]Value conversion failed for column 'name'. Expected type: int(11), received value: \"a\". Reason: [types:1292]Truncated incorrect DOUBLE value: 'a'.",
		"[Lightning:Restore:ErrEncodeKV]when encoding 1-th data row in this chunk: [Import:ErrCastValue]Value conversion failed for column 'name'. Expected type: int(11), received value: \"a\". Reason: [types:1292]Truncated incorrect DOUBLE value: 'a'.",
		"[Import:ErrCastValue]Value conversion failed for column 'c1'. Expected type: tinyint(4), received value: ?. Reason: out of range.",
		"[Import:ErrCastValue]Value conversion failed for column 'c1'. Expected type: tinyint(4), received value: ‹\"BAD\"›. Reason: out of range.",
		"[Lightning:Restore:ErrRestoreTable]restore table `projectmanagement`.`issue_seq` failed: ALTER TABLE `projectmanagement`.`issue_seq` AUTO_INCREMENT=2: alter table auto_increment failed: Error 1146 (42S02): Table 'projectmanagement.issue_seq' doesn't exist",
		"[Lightning:Loader:ErrInvalidSchemaFile]invalid table schema file, duplicated item - test-schema-column-number-mismatch/test.a-schema.sql",
		"[Lightning:Common:ErrUnknown]in file at offset 0: failed to cast value as int(11) for column `c` (#3)",
		"Table `test`.`ttt` exists some duplicate unique keys. The conflicting records have been recorded in table `lightning_task_info`.`conflict_error_v1` for your reference.",
		"Table `test`.`ttt` exists some duplicate keys.",
		"[Lightning:Common:ErrUnknown]unknown error: in file offset 0: unknown value: [0x4006a94090  0x4006a94130 0x4006a94134]",
		"[Lightning:Storage:ErrStorageUnknown]list file failed: unknown value: [0x4002542260 0x4002542264 0x4002542268 0x400254226c]",
		"[Lightning:Common:ErrUnknown]generate table info from SQL error: contact_import_status",
		"[Lightning:Common:ErrUnknown]generate table info from SQL error:   ",
		"[Lightning:Common:ErrUnknown]in file at offset 0: failed to evaluate generated column expression for column `_V$_idx_patient_orgIds_mvi_0`",
	}
	expected := []string{
		"S3AccessDenied: The ciphertext refers to a customer master key that does not exist, does not exist in this region, or you are not allowed to access",
		"BucketRegionError: bucket not in `ap-northeast-1`",
		"RegionUnbalanced: TiDB-Lightning pre-check failed: The region distribution is unbalanced. Please submit a ticket for assistance.",
		"TargetTablesNotEmpty: Target table(s) are not empty. Truncate the target table(s) or import into new empty table(s), then retry.",
		"SchemaNotExists: Target schema 'nft' does not exist. Create it in TiDB first, then retry import.",
		"SyntaxError: CSV format appears invalid. Check separator and quote settings, then retry.",
		"UnknownColumns: unknown columns in CSV file header (id,name,volume,...) for table 'nftHisto'. Please check CSV file header line and re-import data. If the CSV file has a header line, ensure column names of the header line match the target table. If the file doesn't have a header line, disable \"With Header\" in \"CSV Configuration\".",
		"UnknownColumns: unknown columns in CSV file header (c_95747439,aaa_3,1657221693126,...) for table 't'. Please check CSV file header line and re-import data. If the CSV file has a header line, ensure column names of the header line match the target table. If the file doesn't have a header line, disable \"With Header\" in \"CSV Configuration\".",
		"ChecksumMismatch: Please don't modify any data on the target table during the import process, otherwise a checksum mismatch error will lead to import failure. If there is no modification and the import fails, please contact support for assistance.",
		"FileNotExists: data-source-dir 's3://op-data-datastore-poc-prod/csv_latest_versions_20220222_uncompressed/?region=eu-central-1' doesn't exist or contains no files",
		"InternalError: An internal error occurred during import. Please contact support for assistance.",
		"InternalError: An internal error occurred during import. Please contact support for assistance.",
		"ClusterDeleted: Cluster is deleted.",
		"InternalError: An internal error occurred during import. Please contact support for assistance.",
		"FileNotExists: data-source-dir 's3://tidbcloud-samples-testimport/test_import/test?region=us-west-2' doesn't exist or contains no files",
		"S3AccessDenied: User: arn:aws:sts::385595570414:assumed-role/dbaas-shared-a225bc83-import/1656408111895559373 is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::385595570414:rol",
		"InternalError: An internal error occurred during import. Please contact support for assistance.",
		"SchemaNotExists: Target schema 'db' does not exist. Create it in TiDB first, then retry import.",
		"ChecksumMismatch: Please don't modify any data on the target table during the import process, otherwise a checksum mismatch error will lead to import failure. If there is no modification and the import fails, please contact support for assistance.",
		"CreateSchemaError: It is recommended to create the table in TiDB manually and try again later. Details: create schema failed, table: `matrix`.`alerts_labels`, stmt: restore table schema: run create schema job failed: Error 1426: Too big precision 9 specified for column 'TIMESTAMP'. Maximum is 6.",
		"SyntaxError: CSV format appears invalid. Check separator and quote settings, then retry.",
		"CsvHeaderConfigMismatch: Header row detected, but 'With Header' is disabled. Enable 'With Header' or remove the header row, then retry.",
		"InternalError: An internal error occurred during import. Please contact support for assistance.",
		"ChecksumError: Data imported successfully, but an error occurred during checksum process ([tikv:9005]Region is unavailable). Contact support for verification plan for relevant tables.",
		"ReadSourceFileError: Failed to read source file. Error encountered while accessing S3 file.",
		"InternalError: An internal error occurred during import. Please contact support for assistance.",
		"InternalError: An internal error occurred during import. Please contact support for assistance.",
		"CastValueError: test.llama_index_rag_test.csv:0 at offset 25317: failed to cast value as varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin for column `document` (#3): [types:1406]Data Too Long, field len 512, data len 4148 Please correct the source data and retry.",
		"SchemaDataMismatch: Source data is inconsistent with the table schema: affected tables: [`constructai`.`arbetsmoment`]; error details: TiDB schema `constructai`.`arbetsmoment` doesn't have column arbetsmoment, please check table schema and csv file header; TiDB schema `constructai`.`arbetsmoment` doesn't have the default value for arbetsmoment_name. Please add default value for column 'arbetsmoment_name' or choose another column to ignore or add this column in data file.  Please try to fix the source data and try again",
		"AzBlobAccessDenied: GET https://tidbserverless.blob.core.windows.net/serverless-e2e-import. RESPONSE 403: 403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.",
		"GcsAccessDenied: test-import-no-privilege@gcp-serverless-ecosystem-dev.iam.gserviceaccount.com does not have storage.objects.list access to the Google Cloud Storage bucket. Permission 'storage.objects.list' denied on resource (or it may not exist)., forbidden",
		"InternalError: An internal error occurred during import. Please contact support for assistance.",
		"TooManyDuplicatedKeys: There are too many duplicate primary keys or unique keys in the source data. Please check the source data and try again.",
		"CastValueError: [types:1292]Truncated incorrect DOUBLE value: ''. failed to cast value as bigint(20) for column `ward` (#13) Please correct the source data and retry.",
		"CastValueError: test.a.csv:0 at offset 15: failed to cast value as int(11) for column `name` (#1): [types:1292]Truncated incorrect DOUBLE value: 'a' Please correct the source data and retry.",
		"CastValueError: Value conversion failed for column 'c1'. Expected type: tinyint(4), received value: 10000000. Reason: [types:1690]constant 10000000 overflows tinyint. Please correct the source data and retry.",
		"CastValueError: test.a.csv:0 at offset 15: Value conversion failed for column 'name'. Expected type: int(11), received value: \"a\". Reason: [types:1292]Truncated incorrect DOUBLE value: 'a'. Please correct the source data and retry.",
		"CastValueError: when encoding 1-th data row in this chunk: Value conversion failed for column 'name'. Expected type: int(11), received value: \"a\". Reason: [types:1292]Truncated incorrect DOUBLE value: 'a'. Please correct the source data and retry.",
		"CastValueError: Value conversion failed for column 'c1'. Expected type: tinyint(4), received value: ?. Reason: out of range. Please correct the source data and retry.",
		"CastValueError: Value conversion failed for column 'c1'. Expected type: tinyint(4), received value: ‹\"BAD\"›. Reason: out of range. Please correct the source data and retry.",
		"NoSuchTable: restore table `projectmanagement`.`issue_seq` failed: ALTER TABLE `projectmanagement`.`issue_seq` AUTO_INCREMENT=2: alter table auto_increment failed: Error 1146 (42S02): Table 'projectmanagement.issue_seq' doesn't exist",
		"InvalidSchemaFile: invalid table schema file, duplicated item - test-schema-column-number-mismatch/test.a-schema.sql",
		"CastValueError: in file at offset 0: failed to cast value as int(11) for column `c` (#3) Please correct the source data and retry.",
		"DuplicatedUniqueKeys: Table `test`.`ttt` exists some duplicate unique keys. The conflicting records have been recorded in table `lightning_task_info`.`conflict_error_v1` for your reference.",
		"DuplicatedUniqueKeys: Table `test`.`ttt` exists some duplicate keys.",
		"NotSupportedParquetType: in file offset 0: unknown value: [0x4006a94090  0x4006a94130 0x4006a94134]",
		"NotSupportedParquetType: unknown value: [0x4002542260 0x4002542264 0x4002542268 0x400254226c]",
		"SyntaxError: Failed to parse table schema SQL for table 'contact_import_status'. Please check the table schema SQL and retry.",
		"SyntaxError: Failed to parse table schema SQL. Please check the table schema SQL and retry.",
		"GeneratedColumnExpressionError: in file at offset 0: failed to evaluate generated column expression for column `_V$_idx_patient_orgIds_mvi_0`. Please check your generated column definitions and source data.",
	}
	for i, errMsg := range errs {
		require.Equal(t, expected[i], SimplifyErrorMsg(errMsg, constant.ImportDataFormatCsv))
	}

	testcases := []struct {
		errMsg     string
		dataFormat constant.DataFormat
		expected   string
	}{
		{
			errMsg:     "[Lightning:Restore:ErrUnknownColumns]unknown columns in header (id,name,volume,volume_usd,market_cap) for table nftHisto",
			dataFormat: constant.ImportDataFormatCsv,
			expected:   "UnknownColumns: unknown columns in CSV file header (id,name,volume,...) for table 'nftHisto'. Please check CSV file header line and re-import data. If the CSV file has a header line, ensure column names of the header line match the target table. If the file doesn't have a header line, disable \"With Header\" in \"CSV Configuration\".",
		},
		{
			errMsg:     "[Lightning:Restore:ErrUnknownColumns]unknown columns in header (_col_0,_col_1,_col_2,_col_3,_col_4,_col_5,_col_6,_col_7,_col_8,_col_9,_col_10,_col_11,_col_12,_col_13,_col_14,_col_15) for table LINEITEM",
			dataFormat: constant.ImportDataFormatParquet,
			expected:   "UnknownColumns: The columns (_col_0,_col_1,_col_2,...) in the Parquet file header do not match the columns of the target table 'LINEITEM'. Please review the Parquet file header, ensure that the column names match those of the target table, and re-import the data.",
		},
	}

	for _, tc := range testcases {
		require.Equal(t, tc.expected, SimplifyErrorMsg(tc.errMsg, tc.dataFormat))
	}
}

func TestExtractUnknownColumnsForDisplay(t *testing.T) {
	testCases := []struct {
		CaseName          string
		Message           string
		ExpectedColumns   []string
		ExpectedTableName string
	}{
		{
			CaseName:          "short list",
			Message:           "[Lightning:Restore:ErrUnknownColumns]unknown columns in header (id,name,volume,volume_usd,market_cap) for table nftHisto",
			ExpectedColumns:   []string{"id", "name", "volume", "volume_usd", "market_cap"},
			ExpectedTableName: "nftHisto",
		},
		{
			CaseName:          "long list",
			Message:           "[Lightning:PreCheck:ErrCheckDataSource]check data source error: [Lightning:Restore:ErrUnknownColumns]unknown columns in header (c_95747439,aaa_3,1657221693126,q25jdq8dl5upmih2se7zw4dbctpr6uosxmt9hx8bchwuetmzy0fqscozruyfwanxlblqctsbq0xyp942gnt9wu2aawv56hxvgdifam4tlisqj0kw0kyd7j4pjvbsxfy64ipkmgfbokne120lwksrvjndj71g6wrhmtpjlgubh3q7sfxbr7htipe5j36kjwcp4iyezedcv5rxkwem0gpfzl0yjkvvafykixbyfa5aem6vr4qrbqjqq5ngyd7guthqycw5njilhtelrzvuw7o1kif9w6lojyv2u3erfar66zbxyvo8cmancnmn6hzg8xz1fqbgyjzqj9y2boliwfvkrpaloegy9qezslwnrgs3fpjnevcdxrwvpir7gqfvuibwwsk12nw0on61xe3vbwfg38zmamdkgy87bdycbrufraffoz4zk579icpq5f9rqlntdhuqfwwiam5pztrpabywdzzvhdig4npktbdmpbuyszmzhx0rqabst9ovffk3nuvr9wmd3zpeoj59tiz6qmzc0ejrqvuqqfelilokpsmzompeeowviixfryrrmoucilobtij8vxc6lz1eefhxowfwlc58dwkfrnx3ekxwxlececebgqryplem1pouj9gdfa95gpdq85m5mev8tat17ebfru628bh3e6e02ht4g6nnb8zxwvluccfawntu3kmwrsim96dbq7cawtdbzcdyzjysp8tsmm4fpkiv4bhushgcokh2btvferonl7rprqebtcraxrgjlngt5o0ptutwd8m41bh15fphpb9jemoemaanxzcattvfhnneqwdzx1c31nurcyisexsljzylcrqrkgm7uok4wkxvsa3hzz3jxkpmapj6sbwebwphh46t7jbzsxl1vb7vxqgmqz9phsvu30ch460vcdybnvkjmsrjgefloxmuclgd4f2ggyaskxaw1seezbman62vrfgdbifcjp) for table t",
			ExpectedColumns:   []string{"c_95747439", "aaa_3", "1657221693126", "..."},
			ExpectedTableName: "t",
		},
		{
			CaseName:          "abnormal message",
			Message:           "abcdefg",
			ExpectedColumns:   nil,
			ExpectedTableName: "",
		},
		{
			CaseName:          "missing table name",
			Message:           "[Lightning:Restore:ErrUnknownColumns]unknown columns in header (id,name,volume,volume_usd,market_cap) for table    ",
			ExpectedColumns:   []string{"id", "name", "volume", "volume_usd", "market_cap"},
			ExpectedTableName: "",
		},
		{
			CaseName:          "missing columns",
			Message:           "[Lightning:Restore:ErrUnknownColumns]unknown columns in header () for table t",
			ExpectedColumns:   nil,
			ExpectedTableName: "t",
		},
		{
			CaseName:          "space column names",
			Message:           "[Lightning:Restore:ErrUnknownColumns]unknown columns in header (id, name,  volume,  volume_usd, market_cap) for table   t  ",
			ExpectedColumns:   []string{"id", "name", "volume", "volume_usd", "market_cap"},
			ExpectedTableName: "t",
		},
	}
	for _, tc := range testCases {
		t.Run(tc.CaseName, func(t *testing.T) {
			cols, tableName := extractUnknownColumnsForDisplay(tc.Message)
			require.Equal(t, tc.ExpectedColumns, cols)
			require.Equal(t, tc.ExpectedTableName, tableName)
		})
	}
}

func TestSimplifyErrorMsg_DefaultUnknownError(t *testing.T) {
	testcases := []struct {
		name     string
		errMsg   string
		expected string
	}{
		{
			name:     "strip lightning prefix for unknown error",
			errMsg:   "[Lightning:Restore:ErrTotallyUnknown]something unexpected happened",
			expected: "something unexpected happened",
		},
		{
			name:     "keep raw message when no lightning prefix",
			errMsg:   "some plain unknown error",
			expected: "some plain unknown error",
		},
	}

	for _, tc := range testcases {
		t.Run(tc.name, func(t *testing.T) {
			got := SimplifyErrorMsg(tc.errMsg, constant.ImportDataFormatCsv)
			require.Equal(t, tc.expected, got)
		})
	}
}
