Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

2.5 KB
Raw Blame
import { create, fromBinary, toBinary } from "@bufbuild/protobuf";
import { describe, expect, it } from "vitest";
import { findTagMetadata, mergeTagCounts } from "@/lib/tag";
import { UserSetting_TagMetadataSchema, UserSetting_TagsSettingSchema, UserStatsSchema } from "@/types/proto/api/user_service_pb";

describe("exact tag keys", () => {
  it("aggregates names that collide with Object prototype properties", () => {
    const source = Object.fromEntries([
      ["toString", 1],
      ["constructor", 2],
      ["__proto__", 3],
    ]);
    const counts = mergeTagCounts(source, { toString: 4 });

    expect(Object.getPrototypeOf(counts)).toBeNull();
    expect(counts.toString).toBe(5);
    expect(counts.constructor).toBe(2);
    expect(counts.__proto__).toBe(3);
  });

  it("preserves exact prototype-shaped tag keys through protobuf maps", () => {
    const source = Object.fromEntries([
      ["normal", 1],
      ["constructor", 2],
      ["__proto__", 3],
    ]);
    const encoded = toBinary(UserStatsSchema, create(UserStatsSchema, { tagCounts: source }));
    const decoded = fromBinary(UserStatsSchema, encoded);
    const counts = mergeTagCounts(decoded.tagCounts);

    expect(Object.keys(decoded.tagCounts).sort()).toEqual(["__proto__", "constructor", "normal"]);
    expect(counts.normal).toBe(1);
    expect(counts.constructor).toBe(2);
    expect(counts.__proto__).toBe(3);
  });

  it("preserves exact prototype-shaped metadata keys when creating tag settings", () => {
    const source = Object.fromEntries([
      ["normal", { blurContent: false }],
      ["constructor", { blurContent: true }],
      ["__proto__", { blurContent: true }],
    ]);
    const created = create(UserSetting_TagsSettingSchema, { tags: source });
    const decoded = fromBinary(UserSetting_TagsSettingSchema, toBinary(UserSetting_TagsSettingSchema, created));

    expect(Object.keys(created.tags).sort()).toEqual(["__proto__", "constructor", "normal"]);
    expect(Object.keys(decoded.tags).sort()).toEqual(["__proto__", "constructor", "normal"]);
    expect(decoded.tags.normal.blurContent).toBe(false);
    expect(decoded.tags["constructor"].blurContent).toBe(true);
    expect(decoded.tags.__proto__.blurContent).toBe(true);
  });

  it("does not treat inherited properties as exact metadata", () => {
    const metadata = create(UserSetting_TagMetadataSchema, { blurContent: true });
    const setting = create(UserSetting_TagsSettingSchema, { tags: { ".*": metadata } });

    expect(findTagMetadata("toString", setting)).toBe(metadata);
  });
});