<commit_before>module Imap::Backup
  describe Serializer::Mbox do
    subject { described_class.new(folder_path) }

    let(:folder_path) { "folder_path" }
    let(:pathname) { "folder_path.mbox" }
    let(:exists) { true }
    let(:file) { instance_double(File, truncate: nil, write: nil) }

    before do
      allow(File).to receive(:exist?).and_call_original
      allow(File).to receive(:exist?).with(pathname) { exists }
      allow(File).to receive(:open).with(pathname, "ab").and_yield(file)
      allow(File).to receive(:read).with(pathname) { existing.to_json }
    end

    describe "#valid?" do
      context "when the mailbox exists" do
        it "is true" do
          expect(subject.valid?).to be true
        end
      end

      context "when the mailbox doesn't exist" do
        let(:exists) { false }

        it "is false" do
          expect(subject.valid?).to be false
        end
      end
    end

    describe "#append" do
      it "appends the message" do
        subject.append("message")

        expect(file).to have_received(:write).with("message")
      end
    end

    describe "#length" do
      let(:stat) { instance_double(File::Stat, size: 99) }

      before { allow(File).to receive(:stat) { stat } }

      it "returns the length of the mailbox file" do
        expect(subject.length).to eq(99)
      end
    end

    describe "#pathname" do
      it "is the folder_path plus .mbox" do
        expect(subject.pathname).to eq("folder_path.mbox")
      end
    end

    describe "#rename" do
      context "when the mailbox exists" do
        let(:exists) { true }

        before do
          allow(File).to receive(:rename)
<commit_msg>Add spec for Mbox#delete<commit_after>module Imap::Backup
  describe Serializer::Mbox do
    subject { described_class.new(folder_path) }

    let(:folder_path) { "folder_path" }
    let(:pathname) { "folder_path.mbox" }
    let(:exists) { true }
    let(:file) { instance_double(File, truncate: nil, write: nil) }

    before do
      allow(File).to receive(:exist?).and_call_original
      allow(File).to receive(:exist?).with(pathname) { exists }
      allow(File).to receive(:open).with(pathname, "ab").and_yield(file)
      allow(File).to receive(:read).with(pathname) { existing.to_json }
      allow(File).to receive(:unlink).and_call_original
      allow(File).to receive(:unlink).with(pathname)
    end

    describe "#valid?" do
      context "when the mailbox exists" do
        it "is true" do
          expect(subject.valid?).to be true
        end
      end

      context "when the mailbox doesn't exist" do
        let(:exists) { false }

        it "is false" do
          expect(subject.valid?).to be false
        end
      end
    end

    describe "#append" do
      it "appends the message" do
        subject.append("message")

        expect(file).to have_received(:write).with("message")
      end
    end

    describe "#delete" do
      context "when the file exists" do
        it "deletes the file" do
          subject.delete

          expect(File).to have_received(:unlink)
        end
      end

      context "when the file does not exist" do
        let(:exists) { false }

        it "does nothing" do
          subject.delete

          expect(File).to_not have_received(:unlink)
        end
      end
    end

    describe "#length" do
      let(:stat) { instance_double(File::Stat, size: 99) }

      before { allow(File).to receive(:stat) { stat } }

      it "returns the length of the mailbox file" do
        expect(subject.length).to eq(99)
      end
    end

    describe "#pathname" do
      it "is the folder_path plus .mbox" do
        expect(subject.pathname).to eq("folder_path.mbox")
      end
    end

    describe "#rename" do
      context "when the mailbox exists" do
        let(:exists) { true }

        before do
          allow(File).to receive(:rename)

{'fraction_non_alphanumeric': 0.1158787255909558, 'fraction_numerical': 0.0020554984583761563, 'mean_word_length': 2.361830742659758, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2549805', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1151, 'n_words': 318}