Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions internal/analytics/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"
"encoding/json"
"fmt"
"path/filepath"
"time"

"github.com/vishnujayvel/hookwise/internal/core"
Expand Down Expand Up @@ -311,23 +310,6 @@ func (d *DB) ReadAllFeedCache(ctx context.Context) (map[string]interface{}, erro
return result, nil
}

// WriteFeedCacheJSON writes the feed cache data to the JSON bridge file
// at ~/.hookwise/state/status-line-cache.json for the Python TUI (R9.1).
//
// The output format is: {"key1": <data>, "key2": <data>, ...}
// This function respects ARCH-3: it is called from the dispatch process,
// not the daemon.
func WriteFeedCacheJSON(cacheData map[string]interface{}) error {
path := filepath.Join(core.GetStateDir(), "state", "status-line-cache.json")
return core.AtomicWriteJSON(path, cacheData)
}

// WriteFeedCacheJSONTo writes the feed cache data to a specified path.
// This variant is used in tests to avoid writing to the real state directory.
func WriteFeedCacheJSONTo(path string, cacheData map[string]interface{}) error {
return core.AtomicWriteJSON(path, cacheData)
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
Expand Down
84 changes: 0 additions & 84 deletions internal/analytics/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package analytics

import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -317,89 +316,6 @@ func TestReadAllFeedCache_MultipleEntries(t *testing.T) {
assert.Equal(t, true, sessionData["active"])
}

// Test 15: WriteFeedCacheJSON creates JSON file at correct path
func TestWriteFeedCacheJSON_CreatesFile(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "hookwise-feed-json-*")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

outPath := filepath.Join(tmpDir, "status-line-cache.json")

cacheData := map[string]interface{}{
"weather": map[string]interface{}{"temp": float64(72)},
"costs": map[string]interface{}{"total": float64(5.5)},
}

require.NoError(t, WriteFeedCacheJSONTo(outPath, cacheData))

// Verify file exists.
info, err := os.Stat(outPath)
require.NoError(t, err)
assert.True(t, info.Size() > 0)
}

// Test 16: WriteFeedCacheJSON file content matches expected format
func TestWriteFeedCacheJSON_ContentFormat(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "hookwise-feed-json-*")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

outPath := filepath.Join(tmpDir, "status-line-cache.json")

cacheData := map[string]interface{}{
"weather": map[string]interface{}{
"temperature": float64(72),
"unit": "F",
},
"session_info": map[string]interface{}{
"active": true,
"duration": float64(300),
},
}

require.NoError(t, WriteFeedCacheJSONTo(outPath, cacheData))

// Read the file back and verify structure.
raw, err := os.ReadFile(outPath)
require.NoError(t, err)

var parsed map[string]interface{}
require.NoError(t, json.Unmarshal(raw, &parsed))

// Top-level keys should be the cache keys.
assert.Contains(t, parsed, "weather")
assert.Contains(t, parsed, "session_info")

// Verify nested structure.
weather, ok := parsed["weather"].(map[string]interface{})
require.True(t, ok, "weather should be a map")
assert.Equal(t, float64(72), weather["temperature"])
assert.Equal(t, "F", weather["unit"])

session, ok := parsed["session_info"].(map[string]interface{})
require.True(t, ok, "session_info should be a map")
assert.Equal(t, true, session["active"])
assert.Equal(t, float64(300), session["duration"])
}

// Test 17: WriteFeedCacheJSON with empty map creates valid JSON
func TestWriteFeedCacheJSON_EmptyMap(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "hookwise-feed-json-*")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

outPath := filepath.Join(tmpDir, "status-line-cache.json")

require.NoError(t, WriteFeedCacheJSONTo(outPath, map[string]interface{}{}))

raw, err := os.ReadFile(outPath)
require.NoError(t, err)

var parsed map[string]interface{}
require.NoError(t, json.Unmarshal(raw, &parsed))
assert.Empty(t, parsed)
}

// Test 18: Feed cache overwrite replaces existing entry
func TestFeedCache_OverwriteReplaces(t *testing.T) {
db, cleanup := testOpen(t)
Expand Down
Loading