review

review patchsets using your default editor
Log | Files | Refs

cache_test.odin (2718B)


      1 package cache
      2 
      3 import "core:os"
      4 import "core:path/filepath"
      5 import "core:strings"
      6 import "core:testing"
      7 
      8 import "../provider"
      9 
     10 @(test)
     11 answers_replay_across_opens :: proc(t: ^testing.T) {
     12 	context.allocator = context.temp_allocator
     13 	temp := os.temp_directory(context.temp_allocator) or_else ""
     14 	dir, err := os.make_directory_temp(temp, "review-cache-*", context.temp_allocator)
     15 	testing.expect(t, err == nil)
     16 	defer os.remove_all(dir)
     17 	path := filepath.join({dir, "deep", "answers.json"}, context.temp_allocator) or_else ""
     18 
     19 	c, ok := open(false, path)
     20 	testing.expect(t, ok)
     21 	_, hit := get(c, "p", "s", "u")
     22 	testing.expect(t, !hit, "an empty cache replays nothing")
     23 	put(c, "p", "s", "u", provider.Answer{text = "answer", tokens_in = 3, cost = 0.1})
     24 	got, replayed := get(c, "p", "s", "u")
     25 	testing.expect(t, replayed)
     26 	testing.expect_value(t, got.text, "answer")
     27 	testing.expect(t, got.replayed)
     28 	testing.expect_value(t, got.tokens_in, 0)
     29 	save(c)
     30 
     31 	again, opened := open(false, path)
     32 	testing.expect(t, opened)
     33 	got, replayed = get(again, "p", "s", "u")
     34 	testing.expect(t, replayed, "the saved answer is read back")
     35 	testing.expect_value(t, got.text, "answer")
     36 	_, other := get(again, "other", "s", "u")
     37 	testing.expect(t, !other, "another provider's question is another question")
     38 
     39 	fresh, _ := open(true, path)
     40 	_, skipped := get(fresh, "p", "s", "u")
     41 	testing.expect(t, !skipped, "fresh reads nothing")
     42 	testing.expect(t, key("a", "b", "c") != key("a", "b", "d"))
     43 
     44 	// The Go tool's file, with its RFC 3339 times, is read as it stands.
     45 	shared := filepath.join({dir, "go.json"}, context.temp_allocator) or_else ""
     46 	entry := key("p", "s", "u")
     47 	testing.expect(
     48 		t,
     49 		os.write_entire_file(
     50 			shared,
     51 			transmute([]byte)strings.concatenate(
     52 				{
     53 					`{"`,
     54 					entry,
     55 					`":{"text":"from go","in":1,"out":2,"cached":0,"cost":0.1,"at":"2026-09-23T20:55:09.673084018-03:00"}}`,
     56 				},
     57 			),
     58 		) ==
     59 		nil,
     60 	)
     61 	theirs, ok_theirs := open(false, shared)
     62 	testing.expect(t, ok_theirs)
     63 	from_go, replayed_go := get(theirs, "p", "s", "u")
     64 	testing.expect(t, replayed_go, "the Go tool's answer replays")
     65 	testing.expect_value(t, from_go.text, "from go")
     66 	testing.expect_value(t, len(stamp()), 20)
     67 
     68 	// A file that cannot be read is not written over.
     69 	broken := filepath.join({dir, "broken.json"}, context.temp_allocator) or_else ""
     70 	testing.expect(t, os.write_entire_file(broken, transmute([]byte)string("not json")) == nil)
     71 	guarded, _ := open(false, broken)
     72 	put(guarded, "p", "s", "u", provider.Answer{text = "mine"})
     73 	save(guarded)
     74 	kept, _ := os.read_entire_file_from_path(broken, context.temp_allocator)
     75 	testing.expect_value(t, string(kept), "not json")
     76 	testing.expect_value(t, len(key("a", "b", "c")), 64)
     77 }