package invited_code import ( "testing" "github.com/stretchr/testify/assert" ) // 測試 ConvertUseCase 的功能 func TestConverter(t *testing.T) { tests := []struct { name string id int64 base int length int wantCode string wantError bool }{ { name: "Test Case 1: Valid ID 1000000", id: 10000000, base: 10, length: 7, wantCode: "DOOOOOOO", wantError: false, }, { name: "Test Case 2: Valid ID 12345678", id: 12345678, base: 10, length: 8, wantCode: "DWXYGBCH", wantError: false, }, { name: "Test Case 3: Valid ID 98765432", id: 98765432, base: 10, length: 8, wantCode: "EHCBGYXW", wantError: false, }, { name: "Test Case 4: ID too large", id: 10000000000000001, // ID 超過界限 base: 10, length: 8, wantCode: "", wantError: true, }, } converter := MustConverter(10, 8, ConvertTable) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { code, err := converter.EncodeFromNum(tt.id) if tt.wantError { assert.Error(t, err) } else { assert.NoError(t, err) assert.Equal(t, tt.wantCode, code) } if !tt.wantError { // 測試解碼 decodedID, err := converter.DecodeFromCode(code) assert.NoError(t, err) assert.Equal(t, tt.id, decodedID) } }) } } func TestDecodeFromCode(t *testing.T) { converter := MustConverter(10, 8, ConvertTable) tests := []struct { name string code string wantID int64 wantError bool }{ { name: "Decode valid code 1", code: "DOOOOOOO", // 對應於 id 10000000 wantID: 10000000, wantError: false, }, { name: "Decode valid code 2", code: "DWXYGBCH", // 對應於 id 12345678 wantID: 12345678, wantError: false, }, { name: "Decode valid code 3", code: "EHCBGYXW", // 對應於 id 98765432 wantID: 98765432, wantError: false, }, { name: "Decode invalid code with character not in table", code: "UWOXZZZ", // 包含 ZZZ,不在轉換表中 wantID: 0, wantError: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result, err := converter.DecodeFromCode(tt.code) if tt.wantError { assert.Error(t, err) assert.Equal(t, int64(0), result) } else { assert.NoError(t, err) assert.Equal(t, tt.wantID, result) } }) } }