MCPcopy Create free account
hub / github.com/blues/note / TestBulkTimeFieldType23

Function TestBulkTimeFieldType23

lib/bulk_test.go:715–807  ·  view source on GitHub ↗

TestBulkTimeFieldType23 tests the type 23 encoding for _time field which provides 256-second granularity to save 1 byte for satellite

(t *testing.T)

Source from the content-addressed store, hash-verified

713// TestBulkTimeFieldType23 tests the type 23 encoding for _time field
714// which provides 256-second granularity to save 1 byte for satellite
715func TestBulkTimeFieldType23(t *testing.T) {
716 ctx := context.Background()
717 // Simple test: create a template with _time field of type 23,
718 // encode and decode it, verify the time is within expected range
719 t.Run("type_23_time_field", func(t *testing.T) {
720 // Template with _time field of type 23 (3-byte unsigned integer)
721 templateJSON := `{"_time":23,"data":12.1}`
722
723 bulkBody := BulkBody{}
724 bulkBody.NoteFormat = BulkNoteFormatFlexNano
725 bulkBody.NoteTemplate = templateJSON
726 bulkBodyJSON, err := note.JSONMarshal(bulkBody)
727 require.NoError(t, err)
728
729 context, err := BulkEncodeTemplate(bulkBodyJSON)
730 require.NoError(t, err)
731
732 // Create test data
733 data := map[string]interface{}{
734 "data": 42.5,
735 }
736
737 // Get current time just before encoding
738 timeBeforeEncode := time.Now().UTC().Unix()
739
740 output, err := context.BulkEncodeNextEntry(ctx, data, []byte{}, 0, 0, "", "", false)
741 require.NoError(t, err)
742
743 // Decode
744 context, err = BulkDecodeTemplate(ctx, bulkBodyJSON, output)
745 require.NoError(t, err)
746
747 body, _, when, _, _, _, success := context.BulkDecodeNextEntry(ctx)
748 require.True(t, success)
749
750 // The decoded time should be within 256*2 seconds of the current time
751 // (256 for the granularity, *2 for margin)
752 timeDiff := abs(when - timeBeforeEncode)
753 require.LessOrEqual(t, timeDiff, int64(256*2),
754 "Time difference %d should be within %d seconds", timeDiff, 256*2)
755
756 // The time should have 256-second granularity
757 require.Equal(t, int64(0), when%256,
758 "Time %d should be a multiple of 256", when)
759
760 // Check the data value
761 if dataVal, ok := body["data"].(json.Number); ok {
762 f, _ := dataVal.Float64()
763 require.Equal(t, 42.5, f)
764 } else {
765 require.Equal(t, 42.5, body["data"])
766 }
767 })
768
769 // Compare type 23 vs type 14 to show the space savings
770 t.Run("type_23_vs_type_14_size", func(t *testing.T) {
771 ctx := context.Background()
772 // Template with type 14 _time field (4 bytes)

Callers

nothing calls this directly

Calls 6

BulkEncodeTemplateFunction · 0.85
BulkDecodeTemplateFunction · 0.85
absFunction · 0.85
BulkEncodeNextEntryMethod · 0.80
BulkDecodeNextEntryMethod · 0.80
Float64Method · 0.80

Tested by

no test coverage detected