Write device aliases as startup metadata

This commit is contained in:
2026-03-14 23:01:01 +01:00
parent 110388c363
commit e9da0920c6
3 changed files with 84 additions and 5 deletions
+35 -1
View File
@@ -43,6 +43,18 @@ func (writer *fakeWriter) firstBatch() []model.Record {
return writer.batches[0]
}
func (writer *fakeWriter) allBatches() [][]model.Record {
writer.mu.Lock()
defer writer.mu.Unlock()
copyBatches := make([][]model.Record, 0, len(writer.batches))
for _, batch := range writer.batches {
copyBatches = append(copyBatches, append([]model.Record(nil), batch...))
}
return copyBatches
}
func TestServiceFlushesParsedRecords(t *testing.T) {
fake := newFakeWriter()
service := NewService(config.Config{
@@ -66,6 +78,12 @@ func TestServiceFlushesParsedRecords(t *testing.T) {
errCh <- service.Run(ctx)
}()
select {
case <-fake.flushed:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for alias metadata flush")
}
service.Enqueue(model.RawMessage{
Topic: "tele/tasmota_896001/LWT",
Payload: []byte("Online"),
@@ -83,7 +101,23 @@ func TestServiceFlushesParsedRecords(t *testing.T) {
t.Fatal("timed out waiting for pipeline flush")
}
batch := fake.firstBatch()
batches := fake.allBatches()
if len(batches) != 2 {
t.Fatalf("expected 2 flushed batches, got %d", len(batches))
}
metaBatch := batches[0]
if len(metaBatch) != 2 {
t.Fatalf("expected 2 alias metadata records, got %d", len(metaBatch))
}
if metaBatch[0].Measurement != "tasmota_device_meta" {
t.Fatalf("unexpected metadata measurement: %s", metaBatch[0].Measurement)
}
if metaBatch[0].Fields["device_alias"] == "" {
t.Fatalf("expected metadata batch to include device_alias, got %#v", metaBatch[0].Fields["device_alias"])
}
batch := batches[1]
if len(batch) != 2 {
t.Fatalf("expected 2 records in flushed batch, got %d", len(batch))
}