@@ -1206,3 +1206,170 @@ func TestMultiTx(t *testing.T) {
12061206 require .NoError (t , err )
12071207 })
12081208}
1209+
1210+ func TestParseTTLWithDefault (t * testing.T ) {
1211+ t .Run ("Use explicit TTL from request metadata" , func (t * testing.T ) {
1212+ defaultTTL := 600
1213+ s := StateStore {
1214+ ttlAttributeName : "expiresAt" ,
1215+ ttlInSeconds : & defaultTTL ,
1216+ }
1217+
1218+ req := & state.SetRequest {
1219+ Key : "test-key" ,
1220+ Metadata : map [string ]string {
1221+ "ttlInSeconds" : "300" ,
1222+ },
1223+ }
1224+
1225+ ttl , err := s .parseTTL (req )
1226+ require .NoError (t , err )
1227+ require .NotNil (t , ttl )
1228+
1229+ // Should use explicit value (300), not default (600)
1230+ expectedTime := time .Now ().Unix () + 300
1231+ assert .InDelta (t , expectedTime , * ttl , 2 ) // Allow 2 second tolerance
1232+ })
1233+
1234+ t .Run ("Use default TTL when no explicit TTL in request" , func (t * testing.T ) {
1235+ defaultTTL := 600
1236+ s := StateStore {
1237+ ttlAttributeName : "expiresAt" ,
1238+ ttlInSeconds : & defaultTTL ,
1239+ }
1240+
1241+ req := & state.SetRequest {
1242+ Key : "test-key" ,
1243+ Metadata : map [string ]string {},
1244+ }
1245+
1246+ ttl , err := s .parseTTL (req )
1247+ require .NoError (t , err )
1248+ require .NotNil (t , ttl )
1249+
1250+ // Should use default value (600)
1251+ expectedTime := time .Now ().Unix () + 600
1252+ assert .InDelta (t , expectedTime , * ttl , 2 ) // Allow 2 second tolerance
1253+ })
1254+
1255+ t .Run ("No TTL when no default and no explicit TTL" , func (t * testing.T ) {
1256+ s := StateStore {
1257+ ttlAttributeName : "expiresAt" ,
1258+ ttlInSeconds : nil , // No default configured
1259+ }
1260+
1261+ req := & state.SetRequest {
1262+ Key : "test-key" ,
1263+ Metadata : map [string ]string {},
1264+ }
1265+
1266+ ttl , err := s .parseTTL (req )
1267+ require .NoError (t , err )
1268+ assert .Nil (t , ttl )
1269+ })
1270+
1271+ t .Run ("No TTL when ttlAttributeName is not set" , func (t * testing.T ) {
1272+ defaultTTL := 600
1273+ s := StateStore {
1274+ ttlAttributeName : "" , // TTL not enabled in component
1275+ ttlInSeconds : & defaultTTL ,
1276+ }
1277+
1278+ req := & state.SetRequest {
1279+ Key : "test-key" ,
1280+ Metadata : map [string ]string {
1281+ "ttlInSeconds" : "300" ,
1282+ },
1283+ }
1284+
1285+ ttl , err := s .parseTTL (req )
1286+ require .NoError (t , err )
1287+ assert .Nil (t , ttl ) // Should return nil when TTL not enabled
1288+ })
1289+
1290+ t .Run ("Explicit TTL with value -1" , func (t * testing.T ) {
1291+ defaultTTL := 600
1292+ s := StateStore {
1293+ ttlAttributeName : "expiresAt" ,
1294+ ttlInSeconds : & defaultTTL ,
1295+ }
1296+
1297+ req := & state.SetRequest {
1298+ Key : "test-key" ,
1299+ Metadata : map [string ]string {
1300+ "ttlInSeconds" : "-1" ,
1301+ },
1302+ }
1303+
1304+ ttl , err := s .parseTTL (req )
1305+ require .NoError (t , err )
1306+ require .NotNil (t , ttl )
1307+
1308+ // -1 should result in immediate expiration (now + -1)
1309+ expectedTime := time .Now ().Unix () - 1
1310+ assert .InDelta (t , expectedTime , * ttl , 2 )
1311+ })
1312+
1313+ t .Run ("Default TTL with large value" , func (t * testing.T ) {
1314+ defaultTTL := 86400 // 24 hours
1315+ s := StateStore {
1316+ ttlAttributeName : "expiresAt" ,
1317+ ttlInSeconds : & defaultTTL ,
1318+ }
1319+
1320+ req := & state.SetRequest {
1321+ Key : "test-key" ,
1322+ Metadata : map [string ]string {},
1323+ }
1324+
1325+ ttl , err := s .parseTTL (req )
1326+ require .NoError (t , err )
1327+ require .NotNil (t , ttl )
1328+
1329+ expectedTime := time .Now ().Unix () + 86400
1330+ assert .InDelta (t , expectedTime , * ttl , 2 )
1331+ })
1332+
1333+ t .Run ("Error on invalid TTL value" , func (t * testing.T ) {
1334+ defaultTTL := 600
1335+ s := StateStore {
1336+ ttlAttributeName : "expiresAt" ,
1337+ ttlInSeconds : & defaultTTL ,
1338+ }
1339+
1340+ req := & state.SetRequest {
1341+ Key : "test-key" ,
1342+ Metadata : map [string ]string {
1343+ "ttlInSeconds" : "invalid" ,
1344+ },
1345+ }
1346+
1347+ ttl , err := s .parseTTL (req )
1348+ require .Error (t , err )
1349+ assert .Nil (t , ttl )
1350+ assert .Contains (t , err .Error (), "invalid syntax" )
1351+ })
1352+
1353+ t .Run ("Explicit TTL overrides default in request with empty metadata" , func (t * testing.T ) {
1354+ defaultTTL := 1200
1355+ s := StateStore {
1356+ ttlAttributeName : "expiresAt" ,
1357+ ttlInSeconds : & defaultTTL ,
1358+ }
1359+
1360+ req := & state.SetRequest {
1361+ Key : "test-key" ,
1362+ Metadata : map [string ]string {
1363+ "ttlInSeconds" : "0" ,
1364+ },
1365+ }
1366+
1367+ ttl , err := s .parseTTL (req )
1368+ require .NoError (t , err )
1369+ require .NotNil (t , ttl )
1370+
1371+ // Should use explicit value 0, not default
1372+ expectedTime := time .Now ().Unix ()
1373+ assert .InDelta (t , expectedTime , * ttl , 2 )
1374+ })
1375+ }
0 commit comments