|
| 1 | +package dictionary |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "owl/backend/ent" |
| 10 | + |
| 11 | + _ "github.com/lib-x/entsqlite" |
| 12 | +) |
| 13 | + |
| 14 | +func TestResolveAccessibleDictionaryIDHonorsVisibilityAndEnabledState(t *testing.T) { |
| 15 | + client, ownerID, otherUserID := newLookupTestClient(t, 0) |
| 16 | + ctx := t.Context() |
| 17 | + |
| 18 | + owned, err := client.Dictionary.Create(). |
| 19 | + SetName("owned-dictionary"). |
| 20 | + SetTitle("Owned Dictionary"). |
| 21 | + SetSlug("owned-dictionary"). |
| 22 | + SetMdxPath(filepath.Join(t.TempDir(), "owned.mdx")). |
| 23 | + SetOwnerID(ownerID). |
| 24 | + Save(ctx) |
| 25 | + if err != nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | + public, err := client.Dictionary.Create(). |
| 29 | + SetName("public-dictionary"). |
| 30 | + SetTitle("Public Dictionary"). |
| 31 | + SetSlug("public-dictionary"). |
| 32 | + SetMdxPath(filepath.Join(t.TempDir(), "public.mdx")). |
| 33 | + SetOwnerID(otherUserID). |
| 34 | + SetPublic(true). |
| 35 | + Save(ctx) |
| 36 | + if err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + unicode, err := client.Dictionary.Create(). |
| 40 | + SetName("Kelvin"). |
| 41 | + SetTitle("A Unicode Fold"). |
| 42 | + SetSlug("unicode-fold"). |
| 43 | + SetMdxPath(filepath.Join(t.TempDir(), "unicode.mdx")). |
| 44 | + SetOwnerID(otherUserID). |
| 45 | + SetPublic(true). |
| 46 | + Save(ctx) |
| 47 | + if err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + _, err = client.Dictionary.Create(). |
| 51 | + SetName("kelvin"). |
| 52 | + SetTitle("Z ASCII Fold"). |
| 53 | + SetSlug("ascii-fold"). |
| 54 | + SetMdxPath(filepath.Join(t.TempDir(), "ascii.mdx")). |
| 55 | + SetOwnerID(otherUserID). |
| 56 | + SetPublic(true). |
| 57 | + Save(ctx) |
| 58 | + if err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + _, err = client.Dictionary.Create(). |
| 62 | + SetName("disabled-dictionary"). |
| 63 | + SetTitle("Disabled Dictionary"). |
| 64 | + SetSlug("disabled-dictionary"). |
| 65 | + SetMdxPath(filepath.Join(t.TempDir(), "disabled.mdx")). |
| 66 | + SetOwnerID(ownerID). |
| 67 | + SetEnabled(false). |
| 68 | + Save(ctx) |
| 69 | + if err != nil { |
| 70 | + t.Fatal(err) |
| 71 | + } |
| 72 | + _, err = client.Dictionary.Create(). |
| 73 | + SetName("private-dictionary"). |
| 74 | + SetTitle("Private Dictionary"). |
| 75 | + SetSlug("private-dictionary"). |
| 76 | + SetMdxPath(filepath.Join(t.TempDir(), "private.mdx")). |
| 77 | + SetOwnerID(otherUserID). |
| 78 | + Save(ctx) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + |
| 83 | + svc := NewService(client, "", "", nil, "", 0, "", false, 0, "", "") |
| 84 | + tests := []struct { |
| 85 | + name string |
| 86 | + query string |
| 87 | + want int |
| 88 | + found bool |
| 89 | + }{ |
| 90 | + {name: "owned by internal name", query: " OWNED-DICTIONARY ", want: owned.ID, found: true}, |
| 91 | + {name: "public by title", query: "public dictionary", want: public.ID, found: true}, |
| 92 | + {name: "unicode simple case fold", query: "kelvin", want: unicode.ID, found: true}, |
| 93 | + {name: "disabled", query: "disabled-dictionary"}, |
| 94 | + {name: "other private", query: "private-dictionary"}, |
| 95 | + {name: "empty", query: " "}, |
| 96 | + } |
| 97 | + for _, tt := range tests { |
| 98 | + t.Run(tt.name, func(t *testing.T) { |
| 99 | + got, found, err := svc.ResolveAccessibleDictionaryID(t.Context(), ownerID, tt.query) |
| 100 | + if err != nil { |
| 101 | + t.Fatal(err) |
| 102 | + } |
| 103 | + if got != tt.want || found != tt.found { |
| 104 | + t.Fatalf("got (%d, %t), want (%d, %t)", got, found, tt.want, tt.found) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func BenchmarkResolveAccessibleDictionaryID256(b *testing.B) { |
| 111 | + client, ownerID, _ := newLookupTestClient(b, 256) |
| 112 | + svc := NewService(client, "", "", nil, "", 0, "", false, 0, "", "") |
| 113 | + const target = "Dictionary 255" |
| 114 | + |
| 115 | + b.Run("full_list_and_match", func(b *testing.B) { |
| 116 | + b.ReportAllocs() |
| 117 | + for b.Loop() { |
| 118 | + items, err := svc.ListAccessible(b.Context(), ownerID) |
| 119 | + if err != nil { |
| 120 | + b.Fatal(err) |
| 121 | + } |
| 122 | + found := 0 |
| 123 | + for _, item := range items { |
| 124 | + if strings.EqualFold(item.Name, target) || strings.EqualFold(item.Title, target) { |
| 125 | + found = item.ID |
| 126 | + break |
| 127 | + } |
| 128 | + } |
| 129 | + if found == 0 { |
| 130 | + b.Fatal("target dictionary not found") |
| 131 | + } |
| 132 | + } |
| 133 | + }) |
| 134 | + |
| 135 | + b.Run("select_target_id", func(b *testing.B) { |
| 136 | + b.ReportAllocs() |
| 137 | + for b.Loop() { |
| 138 | + id, found, err := svc.ResolveAccessibleDictionaryID(b.Context(), ownerID, target) |
| 139 | + if err != nil { |
| 140 | + b.Fatal(err) |
| 141 | + } |
| 142 | + if !found || id == 0 { |
| 143 | + b.Fatal("target dictionary not found") |
| 144 | + } |
| 145 | + } |
| 146 | + }) |
| 147 | +} |
| 148 | + |
| 149 | +func newLookupTestClient(tb testing.TB, dictionaryCount int) (*ent.Client, int, int) { |
| 150 | + tb.Helper() |
| 151 | + databasePath := filepath.Join(tb.TempDir(), "lookup.db") |
| 152 | + client, err := ent.Open("sqlite3", "file:"+databasePath+"?_pragma=foreign_keys(1)") |
| 153 | + if err != nil { |
| 154 | + tb.Fatal(err) |
| 155 | + } |
| 156 | + tb.Cleanup(func() { _ = client.Close() }) |
| 157 | + ctx := tb.Context() |
| 158 | + if err := client.Schema.Create(ctx); err != nil { |
| 159 | + tb.Fatal(err) |
| 160 | + } |
| 161 | + owner, err := client.User.Create(). |
| 162 | + SetUsername("owner"). |
| 163 | + SetDisplayName("Owner"). |
| 164 | + SetPasswordHash("test"). |
| 165 | + Save(ctx) |
| 166 | + if err != nil { |
| 167 | + tb.Fatal(err) |
| 168 | + } |
| 169 | + other, err := client.User.Create(). |
| 170 | + SetUsername("other"). |
| 171 | + SetDisplayName("Other"). |
| 172 | + SetPasswordHash("test"). |
| 173 | + Save(ctx) |
| 174 | + if err != nil { |
| 175 | + tb.Fatal(err) |
| 176 | + } |
| 177 | + mdxPath := filepath.Join(tb.TempDir(), "dictionary.mdx") |
| 178 | + for i := range dictionaryCount { |
| 179 | + name := fmt.Sprintf("Dictionary %03d", i) |
| 180 | + _, err := client.Dictionary.Create(). |
| 181 | + SetName(name). |
| 182 | + SetTitle(name). |
| 183 | + SetSlug(fmt.Sprintf("dictionary-%03d", i)). |
| 184 | + SetMdxPath(mdxPath). |
| 185 | + SetOwnerID(owner.ID). |
| 186 | + Save(ctx) |
| 187 | + if err != nil { |
| 188 | + tb.Fatal(err) |
| 189 | + } |
| 190 | + } |
| 191 | + return client, owner.ID, other.ID |
| 192 | +} |
0 commit comments